Home >Web Front-end >JS Tutorial >The difference between setTimeout and setInterval in JS

The difference between setTimeout and setInterval in JS

Guanhui
GuanhuiOriginal
2020-05-29 13:24:162328browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn