在倒数计时器结束时将变量设置为false

我可能只是在这里密集...我有一个可变的timeDiscount页面加载时是true 。 当页面加载时,计数器开始。 在柜台结束时,我将timeDiscount设置为false ...除了这似乎不起作用...

在这个jsFiddle中,单击“单击”字将提醒您当前的timeDiscount状态,即使计数器已停止,点击仍返回true。 这是为什么?

https://jsfiddle.net/df773p9m/4/

function startTimer(duration, display) {
  var timer = duration, minutes, seconds;
  refreshIntervalId = 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.text(minutes + ":" + seconds);

    if (--timer < 0) {
      clearInterval(refreshIntervalId)
      timeDiscount = false;
    }
  }, 1000);
}

jQuery(function ($) {
  var timeDiscount = true
  var fiveMinutes = 5,
    display = $('#time');
  startTimer(fiveMinutes, display);

  $("#discount").click(function() {
    alert(timeDiscount);
  })
});

你有一个范围问题。

在你的代码中, timeDiscount是在页面加载时执行的未命名函数内部声明的( jQuery(function ($) {... )。这个函数只有这个标识符才知道该变量,但是你试图从startTimer函数。

将声明移到未命名函数的外部:

var timeDiscount = true

function startTimer(duration, display) {
  var timer = duration, minutes, seconds;
  refreshIntervalId = 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.text(minutes + ":" + seconds);

    if (--timer < 0) {
      clearInterval(refreshIntervalId)
      timeDiscount = false;
    }
  }, 1000);
}

jQuery(function ($) {
  var fiveMinutes = 5,
    display = $('#time');
  startTimer(fiveMinutes, display);

  $("#discount").click(function() {
    alert(timeDiscount);
  })
});

变量必须是全局的才能在javascript函数中访问它,因此请将其保留在外层

var timeDiscount = true
function startTimer(duration, display) {
  var timer = duration, minutes, seconds;
  refreshIntervalId = 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.text(minutes + ":" + seconds);

    if (--timer < 0) {
      clearInterval(refreshIntervalId)
      timeDiscount = false;
    }
  }, 1000);
}

jQuery(function ($) {
  var fiveMinutes = 5,
    display = $('#time');
  startTimer(fiveMinutes, display);

  $("#discount").click(function() {
    alert(timeDiscount);
  })
});

有两种方法可以使它首先发生:将timeDiscount变量移到函数之外,使其成为如下所示的全局变量:

   var timeDiscount = true
   function startTimer(duration, display) {
   var timer = duration, minutes, seconds;
   refreshIntervalId = 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.text(minutes + ":" + seconds);

   if (--timer < 0) {
     clearInterval(refreshIntervalId)
     timeDiscount = false;
   }
   }, 1000);
}

   jQuery(function ($) {
   var fiveMinutes = 5,
   display = $('#time');
   startTimer(fiveMinutes, display);

   $("#discount").click(function() {
      alert(timeDiscount);
   })
});

或者你可以简单地在你的timeDiscount添加window ,并将其设置为window.timeDiscount (请参阅此链接在JavaScript函数中定义全局变量):

   function startTimer(duration, display) {
   var timer = duration, minutes, seconds;
   refreshIntervalId = 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.text(minutes + ":" + seconds);

   if (--timer < 0) {
     clearInterval(refreshIntervalId)
     timeDiscount = false;
   }
   }, 1000);
}

   jQuery(function ($) {

   window.timeDiscount = true
   var fiveMinutes = 5,
   display = $('#time');
   startTimer(fiveMinutes, display);

   $("#discount").click(function() {
      alert(timeDiscount);
   })
});
链接地址: http://www.djcxy.com/p/95037.html

上一篇: Set variable to false at the end of countdown timer

下一篇: How identify the form submit button clicked in a variable using Jquery