简体   繁体   中英

How does setTimeout function work in javascript?

Can anyone please explain how does setTimeout function work in javascript? I'm attaching a onclick listener to some HTML element and the onclick function provides a call back function there. When I click the HTML element, it redirect to another page. My question here will be the following :

  1. Will the call back function be executed after the click? I did some testing and looks like it get dropped in the middle of some where.
  2. If I put the call back function using setTimeout(function(){...}, 0) , does that mean it will be putting on a background thread and always being fully executed?
  3. Can someone please help explain the mechanism behind the setTimeout function?

Thanks a lot,

Take a look at the MDN page for setTimeout.

MDN setTimeout Documentation

It would really help to see some code implementation but generally a callback with a timeout delay of 0 would call the given callback instantly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM