【问题标题】:Show alert box one minute before the countdown ends在倒计时结束前一分钟显示警报框
【发布时间】:2019-01-16 12:39:57
【问题描述】:

我正在开发一个在线测验模块,测验会在 30 分钟后自动提交。现在我想显示一个警报框,在第 29 分钟通知学生“还有 1 分钟完成测验”。我无法弄清楚如何实现这一点。我的代码如下所示。

$(document).ready(function(){
    function get15dayFromNow() {   
       return new Date(new Date().valueOf() + <?php echo $duration ; ?> * 60 * 1000);
    }

    var $time_spend = $('#time_spend');
    $time_spend.countdown(get15dayFromNow(), function(event) {
        $(this).val(event.strftime('%M:%S'));
    });

    var $clock = $('#clock');
    $clock.countdown(get15dayFromNow(), function(event) {
        $(this).html('Time Left :   '+'00:'+event.strftime('%M:%S'));
    })
    .on('finish.countdown', function() {
        submitForm();
    });

    function submitForm()
    {
        document.getElementById("target").submit();
    }
});

【问题讨论】:

  • 你可以开始一个超时 29 分钟,当它完成 alert() 并开始另一个 1 分钟
  • setInterval(function(){ alert("还有 1 分钟"); },1740000); /* 1740000 等于 29 分钟 */ 一旦页面加载或点击任何按钮,你需要在事件监听器中添加这个
  • 感谢您抽出宝贵时间。我知道必须做什么的基本逻辑。但我的问题是 tpo 如何像上面的代码一样在 jQuery 中实现这一点。 @MichalBieda
  • @Kool-Mind 我想根据 php 变量给出这个 1740000。我的意思是这个时间可以改变,所以我想把时间放在那里作为数据库值。如何做到这一点。

标签: javascript php jquery jquery-countdown


【解决方案1】:

如果您使用这样的倒计时脚本:
https://www.w3schools.com/howto/howto_js_countdown.asp

只需修改 if 语句:

if (distance < WHATEVERYOUWANT) {
    alert("Message");
}

【讨论】:

  • 感谢您抽出宝贵时间。我知道必须做什么的基本逻辑。但我的问题是 tpo 如何像上面的代码一样在 jQuery 中实现这一点。
【解决方案2】:

以下是您如何实现此目的的示例:

function countdown (timer) {
  
 console.log(timer)
  
  if(timer ===5){
    alert('halfWay');
  } else if (timer === 0){
    return;
  } 
  
  setTimeout(() => countdown(timer - 1), 1000);

}

countdown(10);

只需调整实施中的数字即可获得所需的结果。

【讨论】:

    【解决方案3】:

    我会在 php 中使用用户开始测验的时间戳开始一个会话,并通过 AJAX 定期检查该值。如果剩余时间

    【讨论】:

      【解决方案4】:

      也许这可行:

      var $time_spend = $('#time_spend');
      $time_spend.countdown(get15dayFromNow(), function(event) {
         $(this).val(event.strftime('%M:%S'));
         if (event = WHATEVER){
            alert('Message');
         }
      });
      

      【讨论】:

      • 谢谢,。我想知道两件事。一个是您提供的此代码是否必须“添加”到现有代码中?其次,我没有得到任何部分。
      • 只需将 if ( ...} 添加到您的代码中。其余部分是您的一部分。您想要的数字是什么。我不确定什么事件(秒,分钟)是,但你会在运行代码时看到。或者使用 console.log(event) 显示值
      • 感谢您的解释。我想在这个地方使用一个 php 变量来“随便”。如何做到这一点?
      • 将 php 变量写入 jQuery 变量:whatever = ;然后在 if 语句中使用这个变量。
      • 我用过这个。但是现在警告框会在页面加载时弹出。 var $time_spend = $('#time_spend'); $time_spend.countdown(get15dayFromNow(), function(event) { $(this).val(event.strftime('%M:%S')); if (event = ){ alert('消息'); } });
      猜你喜欢
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 2011-10-22
      相关资源
      最近更新 更多