0

I want the below function to be executed synchronously. Initially the 1st set time out should wait for 10 sec and call the function name which waits for 2 sec and returns back. I tried the below.

var ty = [1000, 1500, 200]
for (var i = 0; i < ty.length; i++) {
    setTimeout(function(){
        console.log(i)
    }, ty[i]);        
    name(i);
}

function name(i) {
    setTimeout(function() {
        console.log('the page name is' + i);
    }, 2000)
}
Vinny
  • 865
  • 1
  • 11
  • 25
  • 1
    You cannot. There is no synchronous waiting in js. – Bergi Mar 19 '16 at 22:27
  • 1
    the simple way is to increment the waiting time of the first call, this will do the same effect: var ty = [1000, 1500, 200] var delay =0; for (var i = 0; i < ty.length; i++) { delay+= ty[i]; setTimeout(function(){ console.log(i) }, delay); name(i); } function name(i) { setTimeout(function() { console.log('the page name is' + i); }, 200) } – Hazem Torab Mar 19 '16 at 22:28
  • 1
    Btw, it's *milli*seconds not centiseconds. For 2s, you need to pass `2000` – Bergi Mar 19 '16 at 22:30

0 Answers0