• 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

About

Avatar for CanyonCasa @CanyonCasa started