我想在使用javascript的倒数计时器中10分钟前提醒一个警报框

时间:2016-03-16 11:15:03

标签: javascript

下面的代码是显示会话到期的计时器。我想在到期前的最后10分钟,5分钟和1分钟显示警报。试过很多方法但没有成功。

<div id="timer"></div>
<script type="text/javascript">
function countdown(minutes) {
    var seconds = 60;
    var mins = minutes

function tick() {

    var counter = document.getElementById("timer");
    var current_minutes = mins-1
    seconds--;

    counter.innerHTML = 'Remaining Time  for your session to expire is ' +
current_minutes.toString() + ":" + (seconds < 10 ? "0" : "") +     String(seconds);
    if( seconds > 0 ) {
        setTimeout(tick, 1000);
    } else {

        if(mins > 1){

           // countdown(mins-1);   never reach “00? issue solved:Contributed    by Victor Streithorst
           setTimeout(function () { countdown(mins - 1); }, 1000);

        }

    }
}

    tick();
}
 var config = <?php echo json_encode($time4) ?>;
countdown(config);

</script>

1 个答案:

答案 0 :(得分:1)

您必须添加以下条件:

注意:我已将mins-1移至另一个变量。如果要在传递参数时使用相同的表达式,则必须多次计算mins-1

var newMin = mins - 1;
if (newMin === 1 || newMin === 5 || newMin === 10)
  alert(newMin + " minutes left.");

JSFiddle