The setTimeout () function is called twice in a row.

I am writing a bot that sends alerts at variable intervals. I am using setTimeout () and I have a problem that I cannot understand. Code (simplified):

//Original call to setTimeout():

timeout = setTimeout(issueAlarm, 2147483647); // highest possible interval setTimeout() will accept. 

//In bot:
// dh is a priority queue that keeps timestamps sorted from smaller to larger as the user requests alerts. 
//'mom' is a moment.js variable that holds the most recent request. 
//This block of code checks the PQ and if what the user just entered is smaller than what is already stored, it updates the timeout. 
//This seems to work fine
// First get input from user and store the desired time of first alert in 'mom'. Then:
var nextD = dh.peekNext();
if (nextD.timestamp >= mom.valueOf() ){
    var now = new Date();        
    clearTimeout(timeout);
    timeout = mom.valueOf() - now;
    setTimeout(issueAlarm, timeout);
    }

//issueAlarm function:

function issueAlarm() {
    var next = dh.getNext(); // this pops the first alarm from the queue
    // here goes some code that issues message. Then:
    var peek = dh.peekNext(); // this looks at the next element in the queue without popping it
    if (peek) {
        var now = new Date();
        timeout = peek.timestamp - now;
        setTimeout(issueAlarm, timeout);
        }
    }

As examples of input data: First input entered: set an alert every 8 hours, starting at 5 minutes (“Bob call”)

Second Input Introduced: Set an alert every 8 hours, starting at 4 minutes (“Jane Call”)

After 4 minutes, I correctly receive the “Jane call” (this set is installed from the bot code) After a minute, I correctly receive the “Bob call”, but also I receive the “Jane call” (which should not happen before 8 hours)

, . setTimeout() issueAlarm().

: _idleTimeout: 59994 ( , )

: _idleTimeout: 28739994 ( , 8 , )

: _idleTimeout: 28799991 ( - , )

botFramework. JavaScript node.js . , , , .

, , , . , .

+4
2

, Next, , . , , , . , !

var dh = {};
dh.alarms = [{time: 1000, name:"Jane"},{time:5000, name:"Jane"},{time: 3000, name:"Bob"}];

dh.first = function(){
dh.alarms = dh.alarms.sort(function(a,b){return a.time - b.time});
next = dh.alarms.slice(0,1);
dh.alarms = dh.alarms.slice(1);
return next[0]
}

dh.next = function(){
next = dh.alarms.slice(0,1);
dh.alarms = dh.alarms.slice(1);
return next[0]
}

var timeout = setTimeout(executeAlarm, 2999999);

function start(){
    
  var next = dh.first(); // dh.next(); I thought this is your problem!!
  if(next && next.name){

    clearInterval(timeout);
    timeout = setTimeout(function(){executeAlarm(next)},next.time);
  }
  
}

function executeAlarm(next){
  if(!next || !next.name) clearInterval(timeout)
  document.getElementById("alarms").innerHTML += "<span> Alarm for " + next.name + "</span>";
  start();
}

document.addEventListener( "DOMContentLoaded", start, false );
<div id="alarms"></div>
Hide result
+2

( OP).

, -:

if (nextD.timestamp >= mom.valueOf() ){
        var now = new Date();        
        clearTimeout(timeout);
        timeout = mom.valueOf() - now;
        setTimeout(issueAlarm, timeout);
        }

( - 2 ^ 31 - 1), . , , setTimeout,

timeout = setTimeout(issueAlarm, timeout);

-. clearTimeout(), , , -, (5 , " " ), .

@guest271314 @damianfabian .

0

Source: https://habr.com/ru/post/1655676/


All Articles