setInterval

编程入门 行业动态 更新时间:2024-10-21 06:21:39
本文介绍了setInterval-如何仅触发一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好,我有这段代码片段,即使我选择了一段时间,它们也会触发一些代码.

Hello I have this snippet of code that will fire some even after time that I choose.

问题是,例如,如果我放3秒,它将每3秒触发一次,我需要的是3秒后仅触发一次.

The problem is that if, for example I put 3 seconds it will fire every 3 seconds, what I need is it to fire only once after 3 seconds.

function playSound(timeLeft){ var sendDataTimeout = function(){ alert('OK'); } var intervalReference = 0; clearInterval(intervalReference); intervalReference = setInterval(sendDataTimeout, timeLeft); }

推荐答案

改用setTimeout,它的工作原理几乎相同,但只会触发一次.您有clearTimeout和setTimeout,所以它与setInterval

Use setTimeout instead, it works almost the same but will only fire once. You have clearTimeout and setTimeout so its very similar to setInterval

function playSound(timeLeft){ var sendDataTimeout = function(){ alert('OK'); } setTimeout(sendDataTimeout, timeLeft); }

您不再需要使用clearTimeout.但仅供参考,它存在并且与clearInterval相同.

You don't have to use clearTimeout anymore. But FYI it exist and works the same as clearInterval.

更多推荐

setInterval

本文发布于:2023-11-27 12:20:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:setInterval

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!