js中的setTimeout和setinterval 用法说明

setTimeout("countSecond()", 1000);//countSecond()是函数(方法)

setTimeout 只执行一次函数,需要传递两个参数1是函数名,2是执行的时间 ;如果要循环执行函数,将setTimeout放在被执行函数里面的最后面;

setInterval("countSecond()", 1000);

setInterval  循环执行函数,需要传递两个参数1是函数名,2是执行的时间;

使用:

window.clearTimeout(对象) 可清除已设置的setTimeout对象
window.clearInterval(对象) 可清除已设置的setInterval对象

例:

var ss=setInterval("countSecond()", 1000);
window.clearInterval(ss);

原文地址:https://www.cnblogs.com/dreamwf/p/6148785.html