Nic*_*win 8 javascript settimeout setinterval clearinterval
我一直在使用setTimeout和setInterval,我无法让代码按照我希望的方式执行.我的目标是创建一个setInterval,每隔三秒调用一次,并在十秒后清除它.但是,当我在firebug中运行代码时,我唯一得到的是一个数字,我假设它是setInterval的id,因为每次执行代码时,数字都会增加.
var intID = setInterval(function() {
console.log("I've been called");},3000);
setTimeout(clearInterval(intID), 10000);
Run Code Online (Sandbox Code Playgroud)
Poi*_*nty 17
这个说法:
setTimeout(clearInterval(intID), 10000);
Run Code Online (Sandbox Code Playgroud)
表示"调用函数'clearInterval'传递变量'intID'的值,然后将该值和数字的返回值传递10000给函数'setTimeout'."
换句话说,您正在调用函数"clearInterval",然后将返回的值传递给setTimeout().
相反,传递setTimeout()一个函数:
setTimeout(function() { clearInterval(intID); }, 10000);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6006 次 |
| 最近记录: |