My problem is b()
executing lastly, but it should be 2nd in the order. I want to call a JSON api on every click on a button (and always send new api call), but it should remove the last api message from the div:
$(document).ready(function() {
function test() {
function a() {
$("#message-quote, #message-person").empty();
console.log('p1');
};
function b() {
console.log('p2 - before getJSON');
$.getJSON("http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=1&callback=", function(a) {
$("#message-quote").append(a[0].content)
$("#message-person").append("<p>— " + a[0].title + "</p>")
console.log('p2 - in getJSON');
});
};
function c() {
$("body, .button, .quote-icons a").css("background-color", "red");
$(".quote-text").css("color", "red");
console.log('p3');
};
var d = $.Deferred(),
p = d.promise();
p.then(a()).then(b()).then(c());
d.resolve();
};
$("#getMessage").on("click", function() {
test();
});
});
The order I got is:
"p1"
"p2 - before getJSON"
"p3"
"p2 - in getJSON"