javascript setInterval

前端 未结 8 1609
时光说笑
时光说笑 2020-11-30 12:30

a question. If i use setInterval in this manner:

setInterval(\'doSome();\',60000);

am i safe that the doSome() function is tri

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 12:54

    About "exact time safety": The following code starts UpdateAll at intervals of RefreshInterval milliseconds, with adjustment each second so that one start occurs at each second at the start of the second. There will be a slight delay for the finite speed of the computer, but errors will not accumulate.

    function StartAtEachSecond ()
    {
        var OneSecond  = 1000; // milliseconds
        var MinInteral =   50; // milliseconds, estimated safe interval
        var StartTime = OneSecond - (new Date ()).getMilliseconds (); // Time until next second starts.
        if (StartTime < MinInteral) StartTime += OneSecond
        window.setTimeout (StartAtEachSecond, StartTime + MinInteral); // To set up the second after the next.
        for (var Delay = 0.0; Delay < OneSecond - MinInteral; Delay += RefreshInterval) 
        {
            window.setTimeout (UpdateAll, StartTime + Delay); // Runs during the next second.
        }
    }
    

提交回复
热议问题