Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

alternative to setInterval

var interval = 1000; // ms
var expected = Date.now() + interval;
setTimeout(step, interval);
function step() {
    var dt = Date.now() - expected; // the drift (positive for overshooting)
    if (dt > interval) {
        // something really bad happened. Maybe the browser (tab) was inactive?
        // possibly special handling to avoid futile "catch up" run
    }
    … // do what is to be done

    expected += interval;
    setTimeout(step, Math.max(0, interval - dt)); // take into account drift
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: switch window 
Javascript :: javascript fetch api 
Javascript :: var in js 
Javascript :: mongoose add new field to schema 
Javascript :: lodash filter array objects 
Javascript :: date object js 
Javascript :: get string length javascript 
Javascript :: get number of digits in a number 
Javascript :: javascript zoom image onclick 
Javascript :: ajax django send array 
Javascript :: GET and SET the attribute of an element 
Javascript :: javascript prevent value change in select option 
Javascript :: is odd javascript 
Javascript :: javascript select audio device 
Javascript :: map in js 
Javascript :: Query all object in mongo array to satisy condition 
Javascript :: js rename onclick function 
Javascript :: convert base64 to pdf file javascript 
Javascript :: change text based on dropdown selection javascript 
Javascript :: spawn prop with custom model 
Javascript :: generate svg from javascript 
Javascript :: how to add all files in a director to an array in javascript 
Javascript :: document ready vanilla js 
Javascript :: usememo 
Javascript :: expo location background example 
Javascript :: setTilme out js 
Javascript :: middleware uses 
Javascript :: save data response from fetch as global param js 
Javascript :: remove green lines on google maps js 
Javascript :: yup oneof 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =