I'm trying to craft a simple way to defer subsequent JQuery/JavaScript code execution until after this statement is performed and the variable cache[url]
has the object returned to it by the .load()
operator:
cache[url] = $('<div class="bbq-item"/>').appendTo('.bbq-content').load(url);
This statement occurs in the middle of a hashchange listener function:
$(window).on( 'hashchange', function(e) {
etc.
...and I cannot move the code dependent on the success of the .load()
outside of it.
It does not rely on external PHP, JSON, or anything that the typical AJAX "deferred" and "when" operators seem to thrive upon in the examples I've found online; this is pure DOM interrogation and manipulation via Javascript/JQuery.
I've tried wrapping the code that needs to follow it (and is dependent on its success) by wrapping it in a simple "if" clause, like this:
if (cache[url] = $('<div class="bbq-item"/>').appendTo('.bbq-content').load(url)) {
[...code that is dependent on success of the .load()...]
}
...but that doesn't always work, as the loading takes longer than the evaluation in some cases, it seems.
What would be the best strategy to accomplish this?