This has been asked quite a bit on SO, but I couldn't find a resolution to this exact issue.
I'm hoping to synchronously load two functions before calling another function and believe jQuery's $.when().done()
should work, but I can't figure it out.
Here's some simplified code, and jsFiddle below (in this version, wrapItUp()
fires immediately):
HTML:
<span>-</span>
<a>-</a>
javascript (jQuery):
var num = 0;
function funk1() {
setTimeout(function() {
num++;
$("a").text(num);
}, 750);
};
function funk2() {
setTimeout(function() {
num ++;
$("a").text(num);
}, 1500);
};
function wrapItUp() {
$("span").text(num);
};
$.when(funk1(), funk2()).then(function() {
wrapItUp()
});
jsFiddle: http://jsfiddle.net/8pLg3jcm/29/
I've also tried including the following in the funk()
functions (in this version, wrapItUp()
never fires at all):
var dfd = jQuery.Deferred();
...code...
return dfd.promise();