setInterval() 方法

setInterval()是一个函数

简单示例:

setInterval(function(){
alert("hello world"), },1000);

 意思是每1000毫秒弹出“hello world"

 

字符串的形式调用函数名,这种写法调用函数名是不能传参的:

setInterval("hello()",5000);
  function hello(){
    alert("hello");
  }

把要执行的代码以字符串形式放在setInterval()的参数里,它可以传参数

var a = "hello";
    setInterval("alert('"+a+"')",5000);

不以字符串的形式调用函数名,使用它传递参数相对比较清晰

setInterval(function(){
        hello("hello");
    },5000);

    function hello(word){
        alert(word);
    }

  

 

posted @ 2021-11-21 14:24  梦话!  阅读(1636)  评论(0编辑  收藏  举报