Search code examples
jquerydelaywait

JQuery delay() function being skipped


I'm having trouble getting some javascript and Jquery to delay the appropriate amount of time. I want to change some text, wait 5 seconds, then pop an alert.

Here's the code:

$('#result').html("Record has passed").delay(5000);
alert("Record has passed");

For some reason, the alert is running before jquery changes the #result and waits. Any solutions or anybody else see this problem?

I've tried

setTimeout($('#result').html("Record has passed"), 5000);

as well but still no luck.


Solution

  • jQuery's .delay method only works on animations and queued functions. Try using setTimeout instead.

    $("#result").html("Record has passed");
    setTimeout(function(){
        alert("Record has passed");
    },5000);