Umesh Moghariya Umesh Moghariya - 1 year ago 70
Javascript Question

Does Javascript setTimeout stop other script execution

I have referred to this

Still I have a some not cleared idea.

  • I have a js function which updates a tree, it has checkboxes.
    function fillTree()

  • I have another function which is executed on window.onload which checks for
    selected checkboxes
    function checkSelectedBoxes()

  • Now there are a 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 the fillTree()


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

    checkSelectedBoxes(); //executes only when fillTree finishes