javascriptjquerydelaywaitseconds

jQuery: Wait/Delay 1 second without executing code


I can't get the .delay method working in jQuery:

$.delay(3000); // not working
$(queue).delay(3000); // not working

I'm using a while loop to wait until an uncontrolled changing value is greater than or equal to another and I can't find any way to hault execution for X seconds.


Solution

  • $.delay is used to delay animations in a queue, not halt execution.

    Instead of using a while loop, you need to recursively call a method that performs the check every second using setTimeout:

    var check = function(){
        if(condition){
            // run when condition is met
        }
        else {
            setTimeout(check, 1000); // check again in a second
        }
    }
    
    check();