Wait for the function to finish (using setInterval) and execute the following function
If you want to bind functions on completion, you can use callback functions.
Example:
function first(callback) {
console.log('Running first');
if (callback) {
callback();
}
}
function second() {
console.log('Running second function');
}
first(second);
The first function checks if a callback is being used and then runs. If there is no callback function, nothing happens. This way you can link functions. You can also use anonymous functions.
first(function () {
console.log('This function that will run after the first one);
});
If you are using setTimeout (), you cannot be sure if the previous function completed. The best way would be to use promises.
Understanding Promises
Hope I understood your question correctly. Good luck!
source to share
First of all, setInterval cannot be executed by itself, it will run indefinitely unless you clear it with clearInterval.
But if you have some async action inside your function and you want to wait for it and then call another function, you can just promise it as Avraam Mavridis .
function step1() {
var deferred = $.Deferred();
setTimeout(function () {
alert('I am step 1');
deferred.resolve();
}, 1000);
return deferred.promise();
}
function step2() {
alert('I am step 2');
}
step1().done(step2);
source to share