• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Flow of Code Control

 
Ranch Hand
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a function with a setTimeout of 20 seconds and then statements afterwards. It will continue to process the statements after the setTimeout and then go to the statements in the setTimout call. What is the flow of the code to be expected? Once the 20 seconds gets hit do the statements after the setTimeout continue and then the ones in setTimeout, do the statements stop being processed after the setTimeout then the ones in the setTimeout and back to where it left off, etc..,? Is there a way to say process everything inline and not continue onward? It's fairly easy to get around it but knowing what exactly is going on and how to prevent it would be very helpful and much appreciated. Thanks.

Rob
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The setTimeout has no relationship to anything. It goes off on its own and fires off the code when it gets executed. It has no clue that x,y,and z was siting after the call.

It is a asynch process, not a synch one. If you are looking for a pause/sleep. You need to break your function up into two parts and have the setTimeout call the second piece.

Eric
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think of the setTimout as setting a timer in a different thread. The current thread keeps on executing, and your callback function gets invoked in the timeout thread when the timer expires.
 
Rob Hunter
Ranch Hand
Posts: 838
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the expected informative posts guys. Just one more, do function calls react the same? I "thought" quite awhile back I had 2 function calls after one another too that acted like separate threads. I might have had a setTimeout somewhere in place though but are simple function calls sequentially processed or are they shot of as though threads? Thanks again guys.

Rob
 
Bear Bibeault
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Completely sequentially.
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic