浏览器支持
| 方法 | Chrome | Internet Explorer / Edge | Firefox | Safari | Opera |
|---|---|---|---|---|---|
| setTimeout() | 1.0 | 4.0 | 1.0 | 1.0 | 4.0 |
示例代码
3 秒(3000 毫秒)后弹出 "Hello" :
var myVar;
function myFunction() {
myVar = setTimeout(alertFunc, 3000);}
function alertFunc() {
alert("Hello!");}
在第 2、4、6 秒修改输入框中的文本:
var x = document.getElementById("txt");
setTimeout(function(){ x.value = "2 秒" }, 2000);
setTimeout(function(){ x.value = "4 秒" }, 4000);
setTimeout(function(){ x.value = "6 秒" }, 6000);
使用 clearTimeout() 来阻止函数的执行:
var myVar;
function myFunction() {
myVar = setTimeout(function(){ alert("Hello") }, 3000);
}
function myStopFunction() {
clearTimeout(myVar);
}
推荐教程:《》
凉官灰 
![[爱了]](/js/img/d1.gif)
![[尴尬]](/js/img/d16.gif)