美文网首页
超时定时器setTimeout和间歇定时器setInterval

超时定时器setTimeout和间歇定时器setInterval

作者: 报告老师 | 来源:发表于2017-10-04 15:42 被阅读62次

1.setTimeout方法,可以实现类似多线程的效果

参数:code(some JavaScript code),定时执行时间,单位默认为毫秒

返回值:定时器id值

说明:clearTimeout方法可以取消。    setTimeout。

setTimeout(alerthello,2000);

fuction alerthello(){

    alert(“hello”);

  }

//浏览器打开后2S后弹出“hello”,且只执行一次

2.setInterval方法,可以实现间歇调用

参数:jscode,time

返回值:定时器id

setInterval(alertworld,2000);

fuction alertworld(){

  alert(“world”);

}

//每2S弹出world,无限执行。

说明:停止setInterval的方法是clearInterval

这两个方法都属于BOM-window对象

相关文章

网友评论

      本文标题:超时定时器setTimeout和间歇定时器setInterval

      本文链接:https://www.haomeiwen.com/subject/uvlcyxtx.html