Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript setInterval in Safari… getting syntax error

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #33016
    brianatlarge
    Member

    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.

    #73285
    SgtLegend
    Member

    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.