0

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.

10
  • Note that the first parameter of the setInterval function should be a reference to the function (and not the call to the function, unless that call returns a reference to another function). You should use setInterval(informUser, X) Commented Dec 3, 2016 at 21:45
  • Thanks, I'll alter it above (if I can) Commented Dec 3, 2016 at 21:46
  • You can alter, but it's also the answer to your question. Update it in your own code and check. It should solve your problem. Commented Dec 3, 2016 at 21:48
  • did you check my last comment? Do you still have problem there? Commented Dec 3, 2016 at 22:02
  • I still have a problem with the code. I'm trying to give a timer two different values based on the computer's internal timer. Commented Dec 3, 2016 at 22:04

2 Answers 2

1

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;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.