Skip to content

Instantly share code, notes, and snippets.

@arnogues
Last active August 29, 2015 13:56
Show Gist options
  • Save arnogues/8847923 to your computer and use it in GitHub Desktop.
Save arnogues/8847923 to your computer and use it in GitHub Desktop.
queue sync or async function. You can put sleep between the functions
function queue() {
var i = 0,
arr = arguments;
(function next() {
var args = arguments;
if (i < arr.length) {
var func = arr[i++];
var funcArgs = [].concat(next, [].slice.call(args));
if (!isNaN(func)) setTimeout(function () {
arr[i++].apply(this, funcArgs)
}, func);
else {
func.apply(this, funcArgs);
}
}
})();
}
queue(
function (next) {
$.ajax(document.location.href, {
success: function (data, textStatus, jqXHR) {
next(data, textStatus, jqXHR);
}
});
},
function (next, data, textStatus, jqXHR) {
// do stuff after the jquery ajax call and get the params pass to the "next" callback in the previous function
next();
},
500, //sleep
function (next) {
// do stuff after 500ms
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment