Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 67637
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: 67637
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.
 
No, tomorrow we rule the world! With this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic