javascript - 倒计时达到 00 时重新加载页面 :00

标签 javascript jquery

我正在尝试组合 2 个 jQuery 函数,如下所示:

HTML

<span id="clock" data-countdown="2020/05/16 10:08:00"></span>

JavaScript

第一个st脚本正在从data-countdown属性中的时间倒计时到00:00:

var date = $('#clock').data('countdown');

$('#clock').countdown(date, function (event) {
    $(this).html(event.strftime('%M:%S'));
});

第二个nd脚本应该在时间到达00:00时重新加载页面:

var text = document.getElementById('clock').innerHTML;

if (text == '00:00') {
   setTimeout(function() {
       window.location.reload()
   }, 1000);
}

我试图将这两个功能结合起来,但是当时间到达 00:00 时页面不会刷新。这是CodePen .

var date = $('#clock').data('countdown');

$('#clock').countdown(date, function (event) {
    $(this).html(event.strftime('%M:%S'));

    var text = document.getElementById('clock').innerHTML;

    if (text == '00:00') {
        setTimeout(function(){
            window.location.reload()
        }, 1000);
    }
});

最佳答案

这是因为您正在检查事件处理程序外部的文本。 text=='00:00' 的当前条件检查只会在脚本加载时执行一次。

这是有效的 JavaScript 函数

var date = $('#clock').data('countdown');
$('#clock').countdown(date, function (event) {
  $(this).html(event.strftime('%M:%S'));

  if(document.getElementById('clock').innerHTML === "00:00") {
    window.location.reload();
  }
});

关于javascript - 倒计时达到 00 时重新加载页面 :00,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61834514/

相关文章:

javascript - 添加数据后,我从 Firestore 获取重复数据。在 React Native 中

javascript - 屏蔽电话号码,鼠标悬停时显示完整电话号码

javascript - 我可以在 IE7 中使用 jQuery ":before"吗?

javascript - 使用 jquery 将属性附加到输入元素

javascript - 强制网络浏览器下载整个页面内容

javascript - 使用 jquery 自动滚动到可滚动弹出 div 的顶部

javascript - Google Sheet 脚本编辑器 - 报告格式的多个 Sumif

javascript - MapStateToProps 在react-redux中返回Null

jquery - 寻求意见,jQuery : (an exercise) I want to join 5x seperate functions into 1x jQuery function

jquery - Java Jax-RS( Jersey )后端和 jQuery 前端 -> 安全性 + session