Execute the setInterval function without delay the first time

后端 未结 14 2283
故里飘歌
故里飘歌 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:29

    Here's a simple version for novices without all the messing around. It just declares the function, calls it, then starts the interval. That's it.

    //Declare your function here
    function My_Function(){
      console.log("foo");
    }    
    
    //Call the function first
    My_Function();
    
    //Set the interval
    var interval = window.setInterval( My_Function, 500 );

提交回复
热议问题