Programmers use timing events to delay the execution of certain code, or to repeat code at a specific interval.

程序员使用时序事件来延迟某些代码的执行,或以特定的时间间隔重复代码。

There are two native functions in the JavaScript library used to accomplish these tasks: setTimeout() and setInterval().

JavaScript库中有两个用于完成这些任务的本机函数: setTimeout()setInterval()

setTimeout (setTimeout)

setTimeout() is used to delay the execution of the passed function by a specified amount of time.

setTimeout()用于将传递的函数的执行延迟指定的时间。

There are two parameters that you pass to setTimeout(): the function you want to call, and the amount of time in milliseconds to delay the execution of the function.

传递给setTimeout()参数有两个:要调用的函数,以及延迟该函数执行的时间(以毫秒为单位)。

Remember that there are 1000 milliseconds (ms) in a 1 second, so 5000 ms is equal to 5 seconds.

请记住,一秒内有1000毫秒(ms),因此5000毫秒等于5秒。

setTimeout() will execute the function from the first argument one time after the specified time has elapsed.

在指定的时间过去之后, setTimeout()将从第一个参数开始执行函数。

Example:

例:

let timeoutID;function delayTimer() {timeoutID = setTimeout(delayedFunction, 3000);
}function delayedFunction() {alert(“Three seconds have elapsed.”);
}

When the delayTimer function is called it will run setTimeout. After 3 seconds (3000 ms) pass, it will execute delayedFunction which will send an alert.

调用delayTimer函数时,它将运行setTimeout 。 经过3秒钟(3000毫秒)后,它将执行delayedFunction ,它将发送警报。

setInterval

setInterval

Use setInterval() to specify a function to repeat with a time delay between executions.

使用setInterval()可以指定一个函数,该函数在两次执行之间会有时间延迟。

Again, two parameters are passed to setInterval(): the function you want to call, and the amount of time in milliseconds to delay each call of the function .

同样,将两个参数传递给setInterval() :您要调用的函数,以及延迟该函数每次调用的时间(以毫秒为单位)。

setInterval() will continue to execute until it is cleared.

setInterval()将继续执行,直到将其清除。

Example:

例:

let intervalID;function repeatEverySecond() {intervalID = setInterval(sendMessage, 1000);
}function sendMessage() {console.log(“One second elapsed.”);
}

When your code calls the function repeatEverySecond it will run setInterval. setInterval will run the function sendMessage every second (1000 ms).

当您的代码调用函数repeatEverySecond ,它将运行setIntervalsetInterval将每秒(1000 ms)运行一次sendMessage函数。

clearTimeout和clearInterval (clearTimeout and clearInterval)

There are also corresponding native functions to stop the timing events: clearTimeout() and clearInterval().

还有相应的本机函数来停止计时事件: clearTimeout()clearInterval()

You may have noticed that each timer function above is saved to a variable. When either the setTimeout or setInterval function runs, it is assigned a number which is saved to this variable. Note that JavaScript does this all in the background.

您可能已经注意到上面的每个计时器功能都保存到一个变量中。 当setTimeoutsetInterval函数运行时,将为其分配一个数字,该数字将保存到该变量中。 请注意,JavaScript会在后台完成所有操作。

This generated number is unique for each instance of timers. This assigned number is also how timers are identified when you want to stop them. For this reason, you must always set your timer to a variable.

对于每个计时器实例,此生成的编号都是唯一的。 当您要停止计时器时,此分配的编号也是如何标识计时器的。 因此,您必须始终将计时器设置为变量。

For clarity of your code, you should always match clearTimeout() to setTimeout() and clearInterval() to setInterval().

为了使代码清晰,应始终将clearTimeout()setTimeout()匹配,并将clearInterval()setInterval()匹配。

To stop a timer, call the corresponding clear function and pass it the timer ID variable that matches the timer you wish to stop. The syntax for clearInterval() and clearTimeout() are the same.

要停止计时器,请调用相应的清除函数,并将与您要停止的计时器相匹配的计时器ID变量传递给它。 clearInterval()clearTimeout()的语法相同。

Example:

例:

let timeoutID;function delayTimer() {timeoutID = setTimeout(delayedFunction, 3000);
}function delayedFunction() {alert(“Three seconds have elapsed.”);
}function clearAlert() {clearTimeout(timeoutID);
}

翻译自: https://www.freecodecamp.org/news/javascript-timing-events-settimeout-and-setinterval/

JavaScript时间事件:setTimeout和setInterval相关推荐

  1. javascript requestAnimationFrame 解决 setTimeout、setInterval 时间不准的方法。

    javascript requestAnimationFrame 解决 setTimeout.setInterval 时间不准的方法. 取代 setInterval <!DOCTYPE html ...

  2. JavaScript 中的 setTimeout 和 setInterval 方法

    有时您不希望函数立即运行.您希望它重新执行,甚至在特定时间间隔后重复运行.JavaScript 为我们提供了两种实现方法: setTimeout 和 setInterval.下面,我们将来理解这两个方 ...

  3. JavaScript中关于setTimeout和setInterval的使用

    两个函数都是可以用来实现一段时间后执行一段javascript代码的效果.两个函数都有两个参数,前面的都是执行表达式,后面的是隔的秒数. 不同的是setInterval会每隔指定的时间段就执行一次代码 ...

  4. js的事件循环机制:同步与异步任务(setTimeout,setInterval)宏任务,微任务(Promise,process.nextTick)...

    javascript是单线程,一切javascript版的"多线程"都是用单线程模拟出来的,通过事件循环(event loop)实现的异步. javascript事件循环 事件循环 ...

  5. 关于Javascript 中 setTimeout和setInterval的总结和思考

    1. JavaScript 单线程 我们通常说,javascript是单线程,指的是解释和执行js代码的引擎是单线程. 而对于浏览器来说,浏览器并不是单线程的,浏览器的线程通常包括:渲染引擎线程(负责 ...

  6. JavaScript———从setTimeout与setInterval到AJAX异步

    setTimeout与setInterval执行 首先我们看一下以下代码打印结果 1 2 3 4 5 6 7 console.log(1); setTimeout(function() { conso ...

  7. Javascript的setTimeOut()和setInterval()的定时器用法

    Javascript用来处理延时和定时任务的setTimeOut和setInterval函数应用非常广泛,它们都用来处理延时和定时任务,比如打开网页一段时间后弹出一个登录框,页面每隔一段时间发送异步请 ...

  8. js 中 的时间类和 setTimeout 和setInterval

    首先贴代码 <!DOCTYPE html> <html><head><meta charset="UTF-8"><title& ...

  9. setTimeout 和 setInterval 的定时时间深入研究

    setInterval() - 间隔指定的毫秒数不停地执行指定的代码(一直执行). setTimeout() - 在指定的毫秒数后执行指定代码(只执行一次). 使用setInterVal: funct ...

最新文章

  1. CSS-home.htm
  2. labview在2048中添加时间滚动条_Axure 教程:不可见滚动条的页面滚动效果
  3. java常用数据类型之间转换
  4. python for arcgis_面向ArcGIS的Python脚本编程 ([美]赞德伯根) 中文pdf扫描版[50MB]
  5. 视不可当:信息图与可视化传播
  6. JSF – PrimeFaces和休眠集成项目
  7. 如何有效开展小组教学_如何有效地开展小组合作学习——数学科主题教研活动...
  8. 关于Websockets问题:
  9. javascript时间戳转换成yyyy-MM-DD格式
  10. unity2019汉化
  11. 5、海康威视摄像头配置和初步测试
  12. 做了6年的Java,java简历包装项目经验
  13. MySQL sql语句中变量应用
  14. CAD等比例缩放图形
  15. 计算机硕士论文难写吗,写不出论文该放弃吗_硕士研究生论文速成法_硕士研究生论文 计算机...
  16. Java学习笔记 | Java异常处理
  17. 耐得住寂寞,拥得了繁华
  18. mac和windows共享键盘鼠标方案
  19. 图片转PDF格式怎么弄?我来教你几个方法
  20. 零基础入门:实时音视频技术基础知识全面盘点

热门文章

  1. 83. 删除排序链表中的重复元素
  2. MySQL优化原理分析及优化方案总结
  3. Swimming Balls
  4. Java-Character String StringBuffer StringBuilder
  5. HDU4911 Inversion 解题报告
  6. javascript通用验证
  7. 使用URLRewriter实现URL重写
  8. [导入]关于OllyDbg 2.0的消息..
  9. 多个摄像机之间的切换
  10. mui 与jquery 同时使用,$冲突解决办法。