0

I have NaN error with following code.

I have timer of 10 seconds to reload page.

When Page reloads first jquery knob gives NaN value and then starts countdown of 10 seconds.

There is problem when seconds are 60. It shows NaN, 0 and then counts from 59.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://anthonyterrien.com/js/jquery.knob.js"></script>

<script>
    var seconds = 10;

    function secondPassed() {

        var minutes = Math.round((seconds - 30) / 60);
        var remainingSeconds = seconds % 60;

        if (remainingSeconds < 10) {
            remainingSeconds = "0" + remainingSeconds;
        }

        var elem = document.getElementById("countdown");
        elem.value = remainingSeconds;

        $("#countdown").val(remainingSeconds).trigger("change");

        if (seconds == 0) {
            clearInterval(countdownTimer);
            location.reload();
        } else {
            seconds--;
        }
    }
    var countdownTimer = setInterval('secondPassed()', 1000);
</script>
<script>
  $(function() {
      $('.dial').knob({

      });
  });
</script>


<input type="text" class="dial timer"   id="countdown"   data-min="0" data-max="60" data-fgColor="#ffec03"data-width="100" data-height="100" data-thickness=".3"  />
3
Contributors
3
Replies
31
Views
2 Years
Discussion Span
Last Post by ryantroop
0

Not clear what you're doing.

minutes are never used

Without seeing this in actin difficult to say, but I'm assumming that you're initial value (60?) is a string representation of a number. If you are using integers, then use parseInt(...) before processing this number. When posting questions on this type of js, it's often an idea to also post the script on something like jsfiddle.

0

script 1 is timer which reloads page after some seconds var seconds = 10;

script 2 is jquery knob to create animnated counter.

Everything works well except that NaN error

0

I dont mean to be a buzz kill... but why not just do a setTimout for 10000ms and reload the page?

setTimeout(function() { location.reload(); }, 10000);

Then, you don't have to keep track of your own delay... :-/
If you want to keep track of it..

function CheckTime()
{
    var iSeconds = 0;
    var oDisplay = document.getElementById("countdown");
    if (oDisplay)
      iSeconds = parseInt(oDisplay.value);

    if (isNaN(iSeconds))
      {
        //first time running
        oDisplay.value = "10";  //or whatever..
        setTimeout(CheckTime, 1000);
      }
    else if (iSeconds == 0)
      {
         location.reload();
      }
    else
      {
         oDisplay.value = iSeconds--;
         setTimeout(CheckTime, 1000);
      }
}

Then run this once onload, and let it take care of itself.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.