Grow your CSS skills. Land your dream job.

setInterval in Safari… getting syntax error

  • # June 6, 2011 at 5:54 pm

    So I have the following javascript function:

    function countdown(elementID, pageurl) {
    var sec = $("#" + elementID + " span").text() || 0; // Grabs the desired countdown time from the span. elementid is the parent id of the span.
    var timer = setInterval(function() {
    $("#" + elementID + " span").text(--sec);
    if (sec == 0) {
    clearInterval(timer);
    window.location = pageurl; // Redirects the user to specified url
    }
    }, 1000);​
    }

    Basically it finds the number in between a span (most likely “3″) and it makes the text “This page will be refreshed in 3…” countdown from 3 to zero, then it sends the user to a specified page.

    It works beautifully in FF, but in Safari, it never counts down. In the FF console, I get no syntax errors, but in the safari console, I’m getting a syntax error on line 9.

    I’m pulling my hair out and can’t seem to find anything wrong with the code. I’ve even commented out a few lines and still can’t get it to work.

    # June 13, 2011 at 6:51 am

    From what i can gather is returning an error because sec is a string not an integer, try the below and it should work fine afterwards.

    var sec = parseFloat($("#" + elementID + " span").text()) || 0;
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".