4

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);
            });
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
ACP
  • 34,682
  • 100
  • 231
  • 371

2 Answers2

4

setTimeout and clearTimeout are native JavaScript methods so they work in jQuery 1.4.2 also – and as such there is no need for equivalents in jQuery.

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
  • @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 Jun 22 '10 at 06:47
3
$(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
  • 231,737
  • 57
  • 305
  • 359
  • 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 Jul 19 '10 at 18:11
  • @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 Jul 19 '10 at 19:40