Home > Web Front-end > JS Tutorial > body text

The difference between setTimeout and setInterval in JS

Guanhui
Release: 2020-05-29 13:24:16
Original
2282 people have browsed it

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);//清除定时器
Copy after login

setInterval

//实时刷新时间单位为毫秒
setInterval('refreshQuery()',8000); 
/* 刷新查询 */
function refreshQuery(){
   $("#mainTable").datagrid('reload',null);
}
Copy after login

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!

Related labels:
js
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template