browser - Why there is no sleep functionality in javascript when there is setTimeout and setInterval? -


why there no such function in javascript sets timeout continuation, saves necessary state (the scope object , execution point), terminates script , gives control browser? after timeout expires browser load execution context , continues script, , have real non browser blocking sleep functionality work if js engine single threaded.

why there still no such functionality in javascript? why have still slice our code functions , set timeouts next step achieve sleep effect?

i think 'sleep'ing not want in browser.

first of might not clear has happen , how browser should behave when sleep.

  • is complete script runtime sleeping? should because have 1 thread running code. happens if other events oocur during sleep? block, , execution continues blocked events fire. cause odd behaviour might imagine (for instance mouse click events fired time, maybe seconds, after actual click). or these events had ignored, lead loss of information.

  • what happen browser? shall wait sleep if user clicks (e.g. close window) button? think not, might call javascript code again (unload) not able called since program execution sleeping.

on second thought sleep sign of poor program design. program/function/you name has task, shall completed possible. have wait result (for instance wait xhr complete) , want continue program execution meanwhile. in case can , should use asynchronous calls. results in 2 advantages:

  • the speed of scripts enhanced (no blocking of other scripts due sleep)
  • the code executed when should , not before or after event (which might lead other problems deadlocks if 2 functions check same condition ...)

... leads problem: imagine 2 or more pieces of code call sleep. hinder if try sleep @ same, maybe unnecessarily. cause lot of trouble when debug, maybe have difficulties in ensuring function sleeps first, because might control behavior somehow.

well think 1 of parts of javascript, sleep not exist. might interesting how multithreaded javascripts perform in browser ;)


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -