Avoiding a javascript race condition

后端 未结 7 2126
醉话见心
醉话见心 2020-12-23 12:26

Here\'s the scenario:

My users are presented a grid, basically, a stripped down version of a spreadsheet. There are textboxes in each row in the grid. When they ch

相关标签:
7条回答
  • 2020-12-23 12:57

    You don't have a race condition, race conditions can not happen in javascript since javascript is single threaded, so 2 threads can not be interfering with each other.

    The example that you give is not a very good example. The setTimeout call will put the called function in a queue in the javascript engine, and run it later. If at that point you click the save button, the setTimeout function will not be called until AFTER the save is completely finished.

    What is probably happening in your javascript is that the onClick event is called by the javascript engine before the onChange event is called.

    As a hint, keep in mind that javascript is single threaded, unless you use a javascript debugger (firebug, microsoft screipt debugger). Those programs intercept the thread and pause it. From that point on other threads (either via events, setTimeout calls or XMLHttp handlers) can then run, making it seem that javascript can run multiple threads at the same time.

    0 讨论(0)
提交回复
热议问题