• changeInterval updates immediately instead of at next timeout event as specified in documentation.
    It may be related to long times (i.e. minutes) but it's hard to debug short intervals with this example.

    The example below includes the console log from a run appended at the bottom.

    /* 
    
    interval bug demo, Espruino board, firmware version 1v81...
    
    example: making a timer that triggers at an interval on a multiple of interval, 
    i.e. every x minutes on the minute.
    
    timer1 times out at 2 minute intervals but exactly 2 minutes from start, not on the minute.
    while timer2 performs as expected. 
    
    */
    
    // callback
    function tick(t) {
      var d = new Date().toString();
      console.log("tick["+t+"]: ",d);
      };
    
    // get current time, define interval, and compute time to next integral interval
    var d = new Date().toString();
    var interval = 120000;  // 2 minutes
    var tinit = parseInt(interval - ((getTime()*1000) % interval),10);
    console.log("start: ", d,tinit,interval);
    
    // call timer first with difference from next sample, afterward a constant interval
    var timer1 = setInterval(function() { tick(1); },tinit);
    //changeInterval, which should take place after tinit timeout, but takes place immediately
    changeInterval(timer1,interval);
    
    // workaround...
    // timeout tinit, then set interval and call initial tick
    var timer2;
    var timer3 = setTimeout(function() {
      timer2 = setInterval(function() { 
        tick(2); },interval);
        tick(2);
      }, tinit);
    
    // console log results below...  
    /*
    
    =undefined
     _____                 _
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v81 Copyright 2015 G.Williams
    >echo(0);
    start:  Sat Nov 28 2015 19:23:12 GMT+0000 47999 120000
    =undefined
    tick[2]:  Sat Nov 28 2015 19:24:00 GMT+0000
    tick[1]:  Sat Nov 28 2015 19:25:12 GMT+0000
    tick[2]:  Sat Nov 28 2015 19:26:00 GMT+0000
    tick[1]:  Sat Nov 28 2015 19:27:12 GMT+0000
    tick[2]:  Sat Nov 28 2015 19:28:00 GMT+0000
    
    */
    
    

    1 Attachment

  • Thanks - I'll change the documentation. I vaguely remember changing it - the reasoning was that if you made a really long interval, it got difficult to change it to anything shorter.

    If you want the old behaviour it's easy enough to fake:

    var interval = setInterval( function() {
      if (newInterval) {
        changeInterval(interval, newInterval);
        newInterval =  undefined;
      }
      // ...
    , 1000 );
    var newInterval = 1000;
    
    
  • What ever it is: at the begin or end of the interval that is setup, it should be the same as in the browser world... if* ES specifies it...

    For the timeouts Iused so fare it was at the end of the interval... so I had to invoke the function once and then start the interval on it to get the desired immediate action and it's repetition after every n time entities. With a dependency on the interval length that would screw up this logic...

  • Well, as far as I know browsers don't have a changeInterval so compliance probably isn't a big problem in this case.

  • Sorry, was not precise enough: I was only talking about setInterval().

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

changeInterval appears to update immediately for long delays

Posted by Avatar for CanyonCasa @CanyonCasa

Actions