I'll-Be-Back
I'll-Be-Back

Reputation: 10828

Execute function between 8AM to 7PM on Mon-Sat?

Using JavaScript, is it possible to execute updateReport() between 8AM to 7PM from Monday to Saturday?

window.setInterval(function() {
        // Update every 5 minutes
        updateReport();
},300000);

Upvotes: 0

Views: 125

Answers (2)

Sachin
Sachin

Reputation: 40970

Do you want something like this

window.setInterval(function() {
  var currentDateTime = new Date();
  var day =  currentDateTime.getDay();

  var validCondition = currentDateTime.getHours() > 7 && currentDateTime.getHours() < 20 && day > 0;

  if(validCondition){
       // Update every 5 minutes
        updateReport();
  }
},300000);

Upvotes: 1

Rick Hoving
Rick Hoving

Reputation: 3575

Yes you can with a number of conditions.
You have to have the webpage open between these hours.
If so, you can do the following code.

setInterval(function(){
    var now = new Date();
    if(now.getDay() !== 0){ //Sunday
        if(now.getHours() >= 8 && now.getHours() <= 19){
            updateReport();   
        }
    }
}, 300000);

But the question is, is this the best solution?
The answer to that is no.
I don't know what updateReport does, but I would look for a more server oriented solution to update the report.
The first solution that comes into mind is using a cronjob .
Or you can create a windows service to execute it.

Upvotes: 2

Related Questions