Page 1 of 1

Timer Display

Posted: Sun Mar 16, 2014 3:16 pm
by Homan Mohammadi

Hi,

I am trying to build a timer that counts down from 60 to 0, and displays the countdown on the page. Upon success of a particular service, I trigger the JS for this timer. I have the following code, which I believe should be working. However, I get an error saying that the secondPassed function is not defined. Do you know what the problem may be?

var seconds = 60;
function secondPassed() {
document.getElementById(‘test_countdown').innerHTML = seconds;
if (seconds == 0) {
clearInterval(countdownTimer);
document.getElementById(‘test_countdown').innerHTML = "Buzz Buzz";
}
else {
seconds--;
}
}
var countdownTimer = setInterval('secondPassed()', 1000);

Here is an image also for your reference:
Image

I don't know why the error / warning signs show up next to the JS. When I first write the code, the signs don't show up. But when I hit "edit event" and then go back to view the code, they show up.

Thank you for your help!


Timer Display

Posted: Sun Mar 16, 2014 10:59 pm
by Illya Stepanov

Hi Homan,

There is next errors and warnings in your code:

  1. Use:
    pre
    Appery("label_seconds").text(seconds);
    /pre
    instead of:
    pre
    document.getElementById(‘test_countdown').innerHTML = seconds;
    /pre

  2. You have wrong setInterval code. Please take a look here for more details: https://developer.mozilla.org/en/docs...

    And finally, right code would be:
    pre
    var seconds = 60;

    var secondPassed = function(){

    Appery("label_seconds").text(seconds);

    if (seconds == 0) {
    clearInterval(countdownTimer);
    Appery("label_seconds").text("Buzz Buzz");
    } else {
    seconds--;
    };
    };

    var countdownTimer = setInterval(secondPassed, 1000);
    /pre


Timer Display

Posted: Mon Mar 17, 2014 2:24 am
by Homan Mohammadi

That was incredibly helpful Illya! Thank you so much!