1.11 JavaScript 常用库:setTimeout与setInterval

1.11 JavaScript 常用库:setTimeout与setInterval

setTimeout(func, delay)

delay毫秒后,执行函数func()。

clearTimeout()

关闭定时器,例如:

let timeout_id = setTimeout(() => {
    
    
    console.log("Hello World!")
}, 2000);  // 2秒后在控制台输出"Hello World"

clearTimeout(timeout_id);  // 清除定时器

setInterval(func, delay)

每隔delay毫秒,执行一次函数func()。
第一次在第delay毫秒后执行。

clearInterval()

关闭周期执行的函数,例如:

let interval_id = setInterval(() => {
    
    
    console.log("Hello World!")
}, 2000);  // 每隔2秒,输出一次"Hello World"

clearInterval(interval_id);  // 清除周期执行的函数

猜你喜欢

转载自blog.csdn.net/qq_42465670/article/details/130603444