setInterval():
按照指定的周期(以毫秒计)来调用函数或计算表达式。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数
示例:
function t(){
console.log("1111");
}
var id = setInterval("t()",1000);
setTimeout():
在指定的毫秒数后调用函数或计算表达式。由 setTimeout() 返回的 ID 值可用作 clearTimeout() 方法的参数
function t(){
console.log("1111");
}
var id = setTimeout("t()",1000);
区别即为,前者是隔一段时间就进行执行,而后者是在经过1000ms后执行一次,然后就不再继续执行。
以上两者如果需要停止,分别使用如下两种:
clearInterval():
取消由 setInterval() 设置的 timeout。
clearTimeout():
取消由 setTimeout() 方法设置的 timeout。