函数返回NaN并且计数不正确

时间:2014-06-27 20:42:58

标签: javascript jquery html

我正在尝试创建一个计算时间倒计时的计数器,但它正在返回NaN而不是倒计时。

我有多个柜台。一个倒计时,另一个倒计时直至stoptime,另一个计数在stoptime之后计算,即Overtime。但是这个人正在回归NaN而其他人则没有工作或计算。

  function fixIntegers(integer) {
      if (integer < 0) integer = 0;
      if (integer < 10) return '0' + integer;
      return '' + integer;
  }

  function TimeCount(difference) {
      var toReturn = {
          weeks: 0,
          days: 0,
          hours: 0,
          minutes: 0,
          seconds: 0
      };
      toReturn.seconds = fixIntegers(difference % 60);
      difference = Math.floor(difference / 60);

      toReturn.minutes = fixIntegers(difference % 60);
      difference = Math.floor(difference / 60);

      toReturn.hours = fixIntegers(difference % 24);
      difference = Math.floor(difference / 24);

      toReturn.days = fixIntegers(difference % 7);
      difference = Math.floor(difference / 7);

      toReturn.weeks = fixIntegers(difference);
      return toReturn;
  }



  var InThisDiv = $(this).parents().eq(0); // Parent

  var NowTime = new Date(); //Time Now
  var StartTime = new Date(InThisDiv.find('.StartTime').val());
  var StopTime = new Date(InThisDiv.find('.StopTime').val());

  if (StartTime < NowTime && NowTime < StopTime) {
      var diff = TimeCount(Math.floor((NowTime - StartTime) / 1000));
      output = (parseInt(diff, 10) + 2);
      console.log(output.weeks);
  } else if (StopTime < NowTime) {
      var output = TimeCount(Math.floor((StopTime - StartTime) / 1000));
  } else if (NowTime > StopTime) {
      var output = TimeCount(Math.floor((NowTime - StopTime) / 1000));
  } else {
      var output = TimeCount(Math.floor((StopTime - NowTime) / 1000));
  }

 console.log(output.weeks);

提前致谢。这是我的小提琴:http://jsfiddle.net/ALjT9/ 编辑: 添加了HTML New FIddle:http://jsfiddle.net/ALjT9/4/

1 个答案:

答案 0 :(得分:0)

你是JS正在寻找和不存在的元素(至少在你的小提琴中)

  var StartTime = new Date(InThisDiv.find('.StartTime').val());
  var StopTime = new Date(InThisDiv.find('.StopTime').val());

您需要确保这些元素位于DOM中并确保它们具有值。