Home >Web Front-end >JS Tutorial >The difference between setTimeout and setInterval in JS
The difference between setTimeout and setInterval in JS
The setTimeout method is to execute a function or expression after the specified number of milliseconds, while the setInterval method It loops to execute the function or expression every specified number of milliseconds until the clearInterval method clears it.
Code difference
setTimeout
function hello(){ alert("hello"); } //使用方法名字执行方法 var t1 = window.setTimeout(hello,1000); var t2 = window.setTimeout("hello()",3000);//使用字符串执行方法 window.clearTimeout(t1);//清除定时器
setInterval
//实时刷新时间单位为毫秒 setInterval('refreshQuery()',8000); /* 刷新查询 */ function refreshQuery(){ $("#mainTable").datagrid('reload',null); }
Recommended tutorial : "JS Tutorial"
The above is the detailed content of The difference between setTimeout and setInterval in JS. For more information, please follow other related articles on the PHP Chinese website!