san*_*han -1 javascript settimeout
通常,如果要稍后取消超时,则将setTimeout分配给变量.
我在控制台中写了一个简单的setTimeout,令我惊讶的是,控制台返回了一个数字.这个号码是什么意思?
<< setTimeout(function(data){console.log(data)},2000,"data passed as arg");
>> 114
Run Code Online (Sandbox Code Playgroud)
它是一个计时器标识符,允许您稍后取消它
const a = setTimeout (console.log, 1000, 'hello A')
const b = setTimeout (console.log, 1000, 'hello B')
clearTimeout (a)
// you won't see 'hello A', because it was canceled
// you will only see 'hello B'Run Code Online (Sandbox Code Playgroud)
它适用于setInterval与clearInterval太
const t = setInterval (console.log, 1000, 'hello world')
setTimeout (clearInterval, 5000, t)
// outputs 'hello world' once per second
// 5 seconds later, the interval timer is canceledRun Code Online (Sandbox Code Playgroud)