Reputation: 6697
Assume this please:
setTimeout(function(){
console.log('one');
}, 3000);
console.log('two');
I want to print one
first, and then two
in the console. current code prints two
first.
How can I stop processing until setTimeout()
done?
Noted that, I want to execute setTimeout()
once. not each 3 sec.
Here is my real code:
var arr = ['mohammad', 'ali', 'zahra', 'fatemeh'],
res = [],
status = true;
function myfunc() {
if (status == true) {
alert('test');
console.log('Engine started fetching a new one!');
status = false;
$('.im_dialogs_search_field').val(arr[0]);
$('.im_dialogs_search_field').trigger('change');
setTimeout(function() {
if ($('li.im_dialog_wrap').length) {
res.push($(this.find('.im_dialog_photo').attr('src')));
} else {
console.log('There is no result for ' + arr[0]);
status = true;
}
}, 3000);
arr.shift();
} else {
console.log('Engine is bussy!');
}
}
When I call myfunc()
function, it always throws Engine is bussy!
. What's wrong?
Upvotes: 0
Views: 68
Reputation: 5469
setTimeout(function(){
console.log('one');
afterTimeout();
}, 3000);
function afterTimeout(){
console.log('two');
}
hope this helps.
Upvotes: 2
Reputation: 7739
move console.log('two')
inside timeout
Try this
setTimeout(function(){
console.log('one');
console.log('two');
}, 3000);
Upvotes: 1