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"  />

Recommended Answers

All 3 Replies

Member Avatar for diafol

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.