Execute the setInterval function without delay the first time

后端 未结 14 2282
故里飘歌
故里飘歌 2020-11-22 16:17

It\'s there a way to configure the setInterval method of javascript to execute the method immediately and then executes with the timer

14条回答
  •  無奈伤痛
    2020-11-22 16:31

    To solve this problem , I run the function a first time after the page has loaded.

    function foo(){ ... }
    
    window.onload = function() {
       foo();
    };
    
    window.setInterval(function()
    {
        foo(); 
    }, 5000);
    

提交回复
热议问题