javascript - jquery从4秒倒计时到0,然后在0处移动到下一个函数

标签 javascript jquery html

我正在尝试编写一种游戏,你有 4 秒钟的时间(口头)回答一个问题,一旦回答完毕,就会进入下一个问题。我需要它来播放音频(不是我在这里问的),然后从 4 秒倒计时到 0,然后显示正确答案并继续。

如何创建一个从 4 秒到 0 秒的倒计时,然后转到下一件事?

我尝试了以下方法但失败了。

    var counter = 4;

    $('#seconds').html(counter);

    function decreaseSeconds(){
        counter--;
        $('#seconds').fadeOut('fast');
        $('#seconds').html(counter).delay(800);
        $('#seconds').fadeIn('fast');
    }

    while ( counter > 0){
         decreaseSeconds()
    }

最佳答案

参见DEMO

var nextFunction = function () {
  alert('Hello.');
};

sec = 4;

interval = setInterval(function () {
  sec--;
  document.getElementById('sec').innerHTML = sec;

  if (sec == 0) {
    clearInterval(interval);
    nextFunction();
  }
}, 1000); ​

关于javascript - jquery从4秒倒计时到0,然后在0处移动到下一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11755392/

相关文章:

javascript - Flickr api返回数据fail() jquery

javascript - JQuery/javascript 将 div 放置在父 div 的任意位置

javascript - 如何通过将高度设置为百分比来在 div 上设置滚动

javascript - 在 Safari (Windows) 中使用 html5/js 音频时出现问题

javascript - 在动态创建的字段上捕获 javascript 事件

javascript - CoffeeScript - 使用参数执行 bash 脚本

javascript - React.js 中父组件中的复选框不会触发子组件中的函数

javascript - Azure 存储 SAS token 在本地主机上工作,但部署在 Azure Kubernetes 上时不起作用

javascript - 旋转时使用 MVVM 更新 Kendo NumericTextBox

html - 将鼠标悬停在链接上时,我想更改 div 和链接的背景颜色