Skip to content

$.wait jQuery extension that wraps setTimeout in a $.Deferred object

Notifications You must be signed in to change notification settings

MoonScript/jquery-wait

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 

Repository files navigation

jquery-wait

$.wait jQuery extension that wraps setTimeout in a Deferred object.

Basic example:

$.wait(500).then(function(){
  console.log('A half-second later.');
});

Passing the context as the 2nd parameter:

$.wait(500, someContext).then(function(){
  this.something(); // "this" is the context parameter that you pass in
});

This utility is similar to _.delay, except that you can cancel the done callbacks by calling .reject() on the returned Deferred object:

var deferred = $.wait(5000);

deferred.done(function(){
  console.log('This won\'t run if the deferred object is rejected within 5 seconds.');
});

if (confirm('Should we cancel the queued action?')) {
  deferred.reject();
}

Use it along side the Promise-enhanced jqXHR object returned from $.ajax:

var jqXHR = $.ajax({
  // ajax options here
});

$.when(jqXHR, $.wait(500)).done(function(){
  console.log('This won\'t run until the AJAX has returned and at least 500 ms have passed.');
});

Use $.when to know when all Deferred objects have been completed.

About

$.wait jQuery extension that wraps setTimeout in a $.Deferred object

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published