<script type='text/javascript' charset='utf-8'>
// set time variables
var currentTimeH = new Date().getHours();
var currentTimeM = new Date().getMinutes();
var currentDay = new Date().getDay();
// check if it's Sunday through Thursday || Friday before 8am
if ( currentDay <=5 || ( currentDay == 6 && currentTimeH <= 8 ) ) {
// check if it's 11pm to 8am || 30 minutes after 10
if ( currentTimeH >= 23 || currentTimeH <= 8 || (currentTimeH == 22 && currentTimeM >= 30) ) {
// Run some code
}
}
</script>
The conditional logic is working for the time of day, but the day of the week is not working. The code is still running everyday of the week. Did I not nest the if statements correctly?
I think you’d need to call that inside some kind of loop that’s running on a timed interval to check whether the conditions you’ve set up are valid. Otherwise, the JS will get run once when the page is loaded, and won’t get looked at again, unless you’ve got something we don’t know calling it.
Note: Also possible I’ve not understood quite what you’re aiming at.
Use setInterval() function to run the code every 1min? 5min? sot that if the conditions that you are already define inside your code are true the the code is going to executing.
setInterval(function(){
// set time variables
var currentTimeH = new Date().getHours();
var currentTimeM = new Date().getMinutes();
var currentDay = new Date().getDay();
// check if it's Sunday through Thursday || Friday before 8am
if ( currentDay <=5 || ( currentDay == 6 && currentTimeH <= 8 ) ) {
// check if it's 11pm to 8am || 30 minutes after 10
if ( currentTimeH >= 23 || currentTimeH <= 8 || (currentTimeH == 22 && currentTimeM >= 30) ) {
// Run some code
}
}
},6000);