After some research, I came across a helpful callback function that can pause the execution of a script until another script is finished. The solution was found here.
dosomething(4, function () {
console.log("finished loop");
});
function dosomething(delay, callback) {
for (var i = 0; i < 100; i++) {
$("body").append(i + "<br>");
}
alert("done");
if (typeof callback == "function") callback();
}
It's worth noting that this method won't work with asynchronous functions, as I discovered.
doAjax(4, function () {
console.log("finished ajax");
});
function doAjax(delay, callback) {
$.ajax({
type: "post",
url: "/echo/html/",
data: {
html: "some content",
delay: 4
},
dataType: 'html',
success: function () {
console.log("ajax done");
}
});
if (typeof callback == "function") callback();
}
While it may seem unnecessary to create custom callbacks for asynchronous requests when jQuery provides built-in functionality, I was curious about how it could be accomplished. Is it as involved as developing an entire promise library?