user3713442
user3713442

Reputation: 508

Changing Javascript setInterval

I'm experimenting with the setInterval and clearInterval method in Javascript but I'm running into a problem. What I'm trying to do is for a function to be called nominally every "x" seconds for most of the time, but at certain intervals, run more frequently. I've enclosed a bit of code which is the incomplete bare bones of something I've got so far which is a simple proof of concept - in this example, every 10 seconds the minutes are displayed, otherwise between 30 and 40 minutes (as dummy values to see if I can get it to work), the display updates every 1 second. But I've realised I can't check to see if the setInterval is still running and how to restart it again (as the "myVar" variable).

<!DOCTYPE html>
<html>
<body>

<p id="demo">Just testing...</p>

<script>

var d = new Date();

myVar = setInterval( informUser, 10000);

function informUser()
{

if ( d.getMinutes()>30 && d.getMinutes()<40 ) // every second
{
    clearInterval(myVar);
    mvVar = setInterval( informUser, 1000);
    document.getElementById("demo").innerHTML = d.getMinutes();     
}
else // else 10 seconds
{
     document.getElementById("demo").innerHTML = d.getMinutes();    
}

}

</script>

</body>
</html> 

As I say the code is very incomplete as I ran into a brick wall! Once I've got something working, I can make it a bit more complex. EDIT:

Thanks for your comments on the syntax but that isn't the whole problem here. What the code does at the moment is output the minutes to the screen every 10 seconds until 31 minutes past the hour have been reached. Then it stops the timer, and starts it using an interval of 1 second. After 9 minutes have gone by, the code stops because the timer isn't started.

What I'd liked to do is in the "else // else 10 seconds" section to see if myVar is null; if it is, then restart the timer for an interval of 10 seconds.

Upvotes: 0

Views: 860

Answers (2)

Ouroborus
Ouroborus

Reputation: 16894

The Date object is being created outside the function and so remains constant.

The timer duration isn't updated in the else block.

Recreating the interval timer in each mode can cause some odd effects due to drift. Rather, create one timer with a timeout that's a greatest common divisor of both required intervals, count its triggering, and base the logic on that count.

interval = setInterval( informUser, 1000);
intervalCount = 0;

function informUser() {
    var minutes = Date.now().getMinutes();
    intervalCount++;

    if(minutes > 30 && minutes < 40) {
      intervalCount = 0;
      document.getElementById("demo").innerHTML = minutes;
    }
    else if (intervalCount >= 10) {
      intervalCount = 0;
      document.getElementById("demo").innerHTML = minutes;
    }

}

Upvotes: 1

Alan
Alan

Reputation: 325

A couple of things make your question unclear.

"see if the setInterval is still running". Basically, it's always running, the only time you stop it is the clearInterval, and then you start it right up again.

Similarly, myVar is never going to be null.

And in the time period from 31 to 39, you really aren't using the "interval" -- you are clearing it and restarting it each time.

How about this:

var d = new Date();

currInterval = 10000;
myVar = setInterval( informUser, currInterval);

function informUser()
{
   document.getElementById("demo").innerHTML = d.getMinutes();     

   intervalShouldBe = 10000;

   if ( d.getMinutes()>30 && d.getMinutes()<40 ) 
       intervalShouldBe = 1000;

   // stop and restart only when interval needs to change
   if(currInterval != intervalShouldBe) {

      clearInterval(myVar);
      myVar = null;
      myVar = setInterval( informUser, intervalShouldBe);
      currInterval = intervalShouldBe;
   }
}

Upvotes: 0

Related Questions