Understanding Event Queue and Call stack in javascript

前端 未结 2 1901
庸人自扰
庸人自扰 2020-11-28 14:04

My curiosity for understanding the concept of \"Event Queue\" and \"Call Stack\" Started when I was solving this question:

var list = readHugeList();

var ne         


        
2条回答
  •  孤独总比滥情好
    2020-11-28 14:39

    Main reason to use call stack is used to know where to go after end of current function. but most language have size limit to call stack so size of call stack is overflow if you call a function repeatly until function is not finished.

    Most implements of setTimeout have queue for save job. and excute them when idle time.

    First nextListItem is call self before itself is not finished. so call stack will be long until end of item list.

    Second nextListItem is call self after finish itself and call stack is clear also. so call stack will be start at empty when nextListItem is called from setTimeout on idle time.

    1. call stack is made for function call history. and event queue is made for save setTimeout job.

    2. see upper explain.

    3. javascript just execute your statement continually. but will be save where this function is called for return to that after function is finished. call stack is used to save a history of called functions.

提交回复
热议问题