Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run next function after setTimeout done

How to make it work? pls help.

function first() {
    setTimeout((function() {
        $('#q').append('first <br>');
    }), 1000);
}
function second() {
    $('#q').append('second <br>');
}
function third() {
    $('#q').append('third <br>');
}
$.when(first()).done(second()).done(third()); 

first() runs as last function, i need as first

Fiddle here: JSFIDDLE

like image 458
bsbak Avatar asked May 21 '15 04:05

bsbak


1 Answers

I am not sure why are you doing this, but if you want to execute them synchronously, you can place the 2nd and 3rd function call inside setTimeout :

function first() {
    setTimeout(function() {
        $('#q').append('first <br>');
        second();
        third();
    }, 1000);
}
function second() {
    $('#q').append('second <br>');
}
function third() {
    $('#q').append('third <br>');
}
first();
like image 186
Zee Avatar answered Sep 23 '22 18:09

Zee