multithreading - Proof that a sleep function is impossible in Javascript? -


put aside discussion of utility of sleep function, know (more likely, confirm) sleep function impossible in javascript.

but of course need restrict resources used build sleep function. mean sleep function satisfies following criteria:

  1. the syntax must be:

    /*doing before sleep*/ sleep(int milliseconds);//completely pause execution /*doing after sleep*/ 
  2. i notice can use trick of "fetching updates other threads", such using new date()to check condition of terminating loop. similar acts, i.e. let thread enter loop controled boolean variable, , boolean variable depends on information of other thread, e.g. system time, server time, etc., should out of scope of discussion. therefore, constraint imposed should be: build sleep function using javascript's native functionality, means resource of timing settimeout().

well, intuitively feel building such function impossible, because single thread can pause loops. however, single thread execution, impossible change boolean variable (which determines condition of exiting loop) within loop.

but feeling, wondering, has there been discussion of actual possibility of such function in js?

that not question. javascript general word similar languages. it's simple imagine (or create) javascript implementation sleep function (useless though). that's 1 thing.

secondly settimeout use system/server time. can't have 1 without other. answer question is: yes, loop time check (again: totally useless). second restriction unrealistic.

the real restriction should be: sleep not use 100% cpu. in case not possible in major js implementation.

all of in context of synchronous sleep. settimeout is sleep. it's asynchronous. why problem?


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -