Can I use jquery done()
on "non-ajax" functions. I get the error Uncaught TypeError: Cannot call method 'done' of undefined
when I try to do something like this.
function countThreeSeconds() {
var counter = 0,
timer = setInterval(function () {
if (counter == 3) {
console.log("All done. That was three seconds.");
window.clearInterval(timer);
} else {
console.log("Not there yet. Counter at: " + counter);
}
counter++;
}, 1000);
}
(function(){
var timer = countThreeSeconds().done(function(){
alert("done");
});
}());
Thanks
JSBIN
Make the non-ajax function return a promise object.
function countThreeSeconds() {
var counter = 0,
deferred = $.Deferred(),
timer = setInterval(function () {
if (counter == 3) {
console.log("All done. That was three seconds.");
window.clearInterval(timer);
deferred.resolve();
} else {
console.log("Not there yet. Counter at: " + counter);
deferred.notify(counter);
}
counter++;
}, 1000);
return deferred.promise();
}
(function(){
var timer = countThreeSeconds().done(function(){
alert("done");
}).progress(function(i){
console.log("in progress...",i);
});
}());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With