javascript interval

后端 未结 3 554
梦谈多话
梦谈多话 2020-11-30 14:13

How can I use interval in js? For example I want to call a function every 5 seconds?



        
3条回答
  •  余生分开走
    2020-11-30 15:14

    setInterval(function(){
      /* your code here */
    }, 5000);
    

    And if you need to pass data to the function, you can do it with a closure:

    setInterval(function(param){
      return function(){
        console.log(param);
      };
    }("hello"), 5000);
    

    will print "hello" to the console.

提交回复
热议问题