Search code examples
javascriptjquerytimedelaywait

timing/waiting in jQuery


I am writting a banner rotator and I want it to cycle among few pictures after few seconds. Does jQuery has something for waiting and then calling the callback? I can see the .delay method, but it doesn't seem to have a callback function. Or maybe I should use a browser-dependant function? Or maybe javascript already contains a function that I need? I am new to javascript :-| .


Solution

  • You can always use the native javascript:

    setTimeout(function(){ /* do your stuff */ }, 1000);
    

    You can have some fun with mixins:

    var timeoutCallback = function() {
        this.foo = 5;
    }
    
    var scheduleTimeout = function(obj, delay) {
        setTimeout(function(){ timeoutCallback.call(obj); }, delay);
    }
    
    var myobj = { foo: 1, bar: 2 }
    scheduleTimeout(myobj, 1000); // myobj.foo becomes 5 after 1000ms delay