Last active
August 29, 2015 14:04
-
-
Save digiguru/2d16bb4abbf5b27a5350 to your computer and use it in GitHub Desktop.
For faking ajax calls in jquery functions I use this...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var echoDataAfterTime = function(echoData, waitTime) { | |
var dfd = new $.Deferred(); | |
waitTime = waitTime || 1000 * 3; //3 second default (it's long but it proves a point) | |
console.log("Waiting for " + waitTime/1000 + " seconds"); | |
setTimeout(function () { | |
console.log("Waited for " + waitTime/1000 + " seconds to return data", echoData); | |
dfd.resolve(echoData); | |
}, waitTime); | |
return dfd.promise(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage suggestion:
You can very easily create a deferred.
output would be...
You can change the length of the wait
will output
//"Waiting for 5 seconds"
//
// ( 5 second pause)
//
//"Waited for 5 seconds to return data", {title: "hello world"}
But if you have a plugin that expects a deferred to get more data, it might look like this:
If the plugin calls "getMoreData" twice in succession the output would look like: