views:

205

answers:

2

Is there any equivalent for setTimeout and clearTimeout functions in jquery 1.4.2.... I found this ex which uses jquery 1.3.2..

var alerttimer = window.setTimeout(function () {
            $alert.trigger('click');
            }, 3000);
            $alert.animate({height: $alert.css('line-height') || '50px'}, 200)
            .click(function () {
              window.clearTimeout(alerttimer);
              $alert.animate({height: '0'}, 200);
            });
+2  A: 
Tatu Ulmanen
@Tatu Is `delay` function in jquery works similar to `settimeout`?
Pandiya Chendur
@Pandiya, the `.delay()` method works a bit differently. Yes, it's a wrapper for `setTimeout` but it is used in queues to delay the running of subsequent functions. For example `$('#foo').slideUp(300).delay(800).fadeIn(400);`. It doesn't help in your case.
Tatu Ulmanen
+1  A: 
$(document.body).delay(3000).show(1, function(){
    // do something
});

that would make use of jQuerys fx queuing to create a timeout. To emulate an interval in that manner, use a function which calls itself in the callback closure.

function repeat(){
     // do something
     $(document.body).delay(5000).show(1, repeat);
}

Use $(document.body).stop() to clear the fx queue and stop the interval.

That works similiar to a javascript setTimeout interval "hack".

(function(){
    alert('I popup every 5 seconds! haha!');
    setTimeout(arguments.callee, 5000);
})();
jAndy
Please note that the .delay() might not happen exactly every 5 seconds. In fact, it will most likely happen at an interval slightly larger than that, due to execution time. The code in `repeat()` takes time to execute, and then the delay is set. It works, but it won't be as exact as setInterval()
Ryan Kinal
@Ryan: You're right there. But nontheless, not timer in `ecma-/javascript` is not exact due to the `UI queue`. What `setTimeout()` for instance causes is, to add some code after a specific time to the `UI queue`. It does not guerantee the immediate execution.
jAndy