Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Javascript setTimeout stop other script execution

Tags:

javascript

I am referring to this. Everything is still not clear.

  • I have a JS function fillTree() which updates a tree, it has checkboxes.
  • I have another function checkSelectedBoxes() which is executed on window.onload which checks for selected checkboxes.
  • Now there are lots of other functions connected.

My question:

  • If I am using setTimeout() will the other script function also stop and wait for my function to finish loading?

What might be the case in this:

function fillTree(){...}
function checkSelectedBoxes(){...}

fillTree(); // This take time to get data. onLoad() doesnt work.
setTimeout(function(){ checkSelectedBoxes()  },5000);

This returns me null values even after increasing the time interval. Does fillTree() pause execution?

like image 878
Umesh Moghariya Avatar asked Apr 21 '12 10:04

Umesh Moghariya


1 Answers

No, setTimeout does not wait for you (hence, JS has no pause function). What setTimeout does is set aside that task at a later time, and allow the next line to be executed. when that timeout is reached , it inserts that task into the execution line. and when no other code is running, it executes the function that was indicated.

what you want to do is give a callback to your fillTree() and execute when it's done.

function fillTree(callback){

    //do something very long

    callback(); //execute passed function when it's done
}

fillTree(function(){
    checkSelectedBoxes(); //executes only when fillTree finishes
})
like image 92
Joseph Avatar answered Oct 19 '22 20:10

Joseph