Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery每秒倒计时_Javascript_Jquery - Fatal编程技术网

Javascript jQuery每秒倒计时

Javascript jQuery每秒倒计时,javascript,jquery,Javascript,Jquery,我试图制作一个jQuery倒计时类型的动画,一旦它达到0,它就会执行一个函数。然而,我有问题,因为我不知道如何去做这件事。我想我应该做一个while循环,然后暂停一秒钟,直到它达到0。然而,似乎不可能暂停一段时间。所以我想知道最好的方法是什么?谢谢 我会使用递归函数 var countDown = function(secondsRemaining){ secondsRemaining -= 1; if(secondsRemaining <= 0){ //e

我试图制作一个jQuery倒计时类型的动画,一旦它达到0,它就会执行一个函数。然而,我有问题,因为我不知道如何去做这件事。我想我应该做一个while循环,然后暂停一秒钟,直到它达到0。然而,似乎不可能暂停一段时间。所以我想知道最好的方法是什么?谢谢

我会使用递归函数

var countDown = function(secondsRemaining){
    secondsRemaining -= 1;
    if(secondsRemaining <= 0){
        //execute
    } else {
        //wait 1 second and call again
        setTimeout(function(){
            countDown(secondsRemaining);
        }, 1000);
    }
}
  • 倒计时
    需要一个HTMLElement来显示它本身以及倒计时的秒数

  • 它返回一个在计数器达到0时解析的承诺

  • 我们可以使用
    。然后
    调用在倒计时完成时应用函数

功能倒计时(elem,s){
返回新承诺(函数(解析){
函数循环{
elem.innerHTML=s
如果(s==0)
解决(elem)
其他的
设置超时(循环,1000,s-1)
}
环路(s)
})
}
倒计时(document.querySelector('#a'),3)(
函数(elem){console.log('done',elem)}
)
倒计时(document.querySelector('#b'),5)(
函数(elem){console.log('done',elem)}
)
倒计时(document.querySelector('#c'),10)(
函数(elem){console.log('done',elem)}
)

代码:

var counter = 10;
var yourFunc = function(){}
var interval = setInterval(function(){
    counter--;
    if(counter <=0){ yourFunc(); clearInterval(interval); }
}, 1000);
var计数器=10;
var yourFunc=函数(){}
var interval=setInterval(函数(){
计数器--;

如果(counter您正在寻找类似此OP的产品吗

它每秒钟执行一次。您可以使用clearInterval(),就像我在“注释”部分中添加的一样,只要您希望它停止

var start=10;//从开始倒计时的时间(秒)
var interval=setInterval(
函数(){
如果(开始==0){
完全();
间隔时间;
}
$(“.update”).html(“倒计时”+开始+”);
开始--;
}, 1000);
函数完成(){
log(“调用回调,start的值为:“+start”);
}

我会使用如下内容:

 $(document).ready(function(){
  var counter=10;
  countDown();

  function countDown(){

    $('#showNumber').text(counter--);
    if(counter>=0) 
      window.setTimeout(countDown,1000)
    else
      otherFunction();
  }

  function otherFunction(){
     $('#showNumber').text('FINISHED!');
  }

});

试试这个,它不需要jQuery

var count = 5;  // Number of times to run 'counter_function'.

// Run 'counter_function' every second (1000ms = 1 second)
var counter = setInterval(function(){
    counter_function()
}, 1000);

// The function to run when 'count' hits 0.
var done_function = function() {
    console.log('done');
}

// The function to run at each interval.
var counter_function = function() {
    console.log('count');
    count--;

    if(count === 0){
        done_function();
        clearInterval(counter);
    }
}

它将每秒打印一次单词“count”,持续5秒,最后一秒还将打印“done”。

您是否正在寻找setInterval()js计时函数?可能会有帮助(js):hense为什么我在第4行添加注释
//execute
。在向下voting之前仔细阅读无规则递归是OP熟悉的一件好事我想你有点过于自信OP不会喜欢它提供的灵活性。问题很模糊,这个解决方案也不复杂。我们让OP决定如何de..@naomik这正是setInterval所做的。@zsawaf这并不完全是
setInterval
所做的。如果没有额外的API调用,setInterval将无限期地循环。
setTimeout
正如godmode在这里使用的那样,它处理终止时更加优雅。它应该倒计时并在计时器到达时调用用户函数他0@naomik因此,将时间间隔设置为您想要的任何值,一旦时间达到0,它就会执行,如果他想退出时间间隔,我就向他提供了这样做的代码。既然您的答案总是计数,那么OP是否应该将
I
设置为负数?
var count = 5;  // Number of times to run 'counter_function'.

// Run 'counter_function' every second (1000ms = 1 second)
var counter = setInterval(function(){
    counter_function()
}, 1000);

// The function to run when 'count' hits 0.
var done_function = function() {
    console.log('done');
}

// The function to run at each interval.
var counter_function = function() {
    console.log('count');
    count--;

    if(count === 0){
        done_function();
        clearInterval(counter);
    }
}