简体   繁体   English

在javascript中添加/减少倒计时的时间

[英]Adding / subtracting time from a countdown in javascript

I'm trying to figure out javascript ... Currently, I am using a countdown timer. 我正在试图找出javascript ...目前,我正在使用倒数计时器。 I'd like to add/subtract 1 minute when the left or right key is pressed. 当按下左或右键时,我想加1 /减1分钟。

I tried the following: 我尝试了以下方法:

  $(document).keydown(function(e) {
    switch(e.which) {
        case 37: // left
          current = parseInt($('#time2').textContent);
          newtime = current + 60;
          countdown_start(newtime)
        break;

        case 39: // right
          alert('right');
        break;

        default: return;
    }
    e.preventDefault(); // prevent the default action (scroll / move caret)
  });

But it has some really funky reaction ... and starts counting down twice...one with a NaNaNaNa... 但它有一些非常时髦的反应...并开始倒数两次......一个有NaNaNaNa ......

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

.background-countdown
  #start-game
    = submit_tag 'Play', id: "start"
  #time2
    60:00
  .test
    asd
  -##start-time
  -#  =text_field_tag 'start-time-input', "60:00", id: "start-time-input"

  #hint-text.white-text

:javascript

  function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    var countdown = setInterval(function () {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.textContent = minutes + ":" + seconds;

        if (--timer < 0) {
          $('#start').show()
          $('#start-time').show()
          clearInterval(countdown)
        }
    }, 1000);
  }
  function countdown_start(sixtyMinutes = 3600) {
    $(document).ready(function () {
      display = document.querySelector('#time2');
      startTimer(sixtyMinutes, display);
    });
  }

  $('#start').click(function() {
    countdown_start()
    $('#start').hide()
    event.preventDefault();
  });

  function get_text(){
    var feedback = $.ajax({
      type: "POST",
      url: "/jquery/update_text",
      async: false
    }).complete(function(){
      setTimeout(function(){get_text();}, 1000);
    }).responseText;
  }

  $(function(){
    get_text();
  });

The problem here might be, that you are using clearInterval right, but you have not correctly saved the returned ID value of the setIntervall method. 这里的问题可能是您正在使用clearInterval,但是您没有正确保存setIntervall方法的返回ID值。

Please have a look at the w3schools.com page: http://www.w3schools.com/jsref/met_win_clearinterval.asp 请查看w3schools.com页面: http//www.w3schools.com/jsref/met_win_clearinterval.asp

They write in a note under the definition: 他们在定义下写了一个注释:

Note: To be able to use the clearInterval() method, you must use a global variable when creating the interval method 注意:为了能够使用clearInterval()方法,必须在创建interval方法时使用全局变量

You save your ID locally so maybe you shouldn't do that. 你在本地保存你的ID,所以也许你不应该这样做。 Instead use a global variable, which just means that you declare it outside the functions section and use that reference over and over again, BUT what you also want to change is your order. 而是使用全局变量,这意味着您在函数部分之外声明它并反复使用该引用,但您还想要更改的是您的订单。 First you are setting an interval and later in the startMethod-function you clear that same interval. 首先,您要设置一个间隔,然后在startMethod函数中清除相同的间隔。 I think if you change the order and make the variable global, it should work. 我认为如果你改变顺序并使变量成为全局变量,它应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM