Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Marl3x26765y@netikras No, the timeout waits in a seperate thread so the for loop will just loop through, thus setting i to 10. After the 1 second wait, it will display 10, ten times.
You can fix this by using let instead of var. -
@Marl3x gotcha!
Js has threads now? I recall it used to rely on alarm() and was strictly single-threaded -
Marl3x26765y@netikras Yeah using thread isn't the right word. It has an event-loop and puts actions there to be executed at a later time. It runs through this loop does what it has to do and continues in search for the next action.
There is a great Video about it: https://youtu.be/cCOL7MC4Pl0
But yeah, you're right. It is single threaded. -
vane110525y@Marl3x I think actual implementation inside browser engine is execute loop to add function into timer linked list and after timeout execute those functions in same thread.
The only other thread I know is requestanimationframe and web workers. -
inaba45895y@netikras it's really misleading but you really only have one thread to execute your code with. However what you do have are functions (like fetch and setTimeout) that the engine you're running JS on can run.
So in the browser when call setTimeout you basically tell the engine (spidermonkey, v8, etc.) "hey can you tell me to run this function after n milliseconds?" and that will happen. That function you need to call will then (thanks to the event loop) be called when the call stack is empty -
@inaba so yeah, I guess that means the alarm() mechanism is still running JS then :)
I got confused when he said "Threads" and "Javascript".
https://perldoc.perl.org/functions/... -
@hack This is the actual real-world answer, there's no reason to have a bunch of setTimeout calls here especially since they all have the same delay. Providing a code sample that would fail code review and expecting people to replace it with another version that would also fail code review sounds like a failing grade for the interviewing company.
-
Root797735yI would indignantly ask them "really? This is the kind of code you write?"
@HollowKitty is right. This is a hard fail. This is also why I loathe whiteboarding: it's full of gimmicks and gotchas like this, it shows the interviewers basically nothing useful, and it's both stressful and needlessly difficult for the interviewee.
Related Rants
I was in a technical interview for a web development position. When it was time for them to choose a question they showed me this:
How can you make this code display 0 to 9 correctly?
for (var i=0; i < 10; i++){
setTimeout(function(){
console.log(i);
}, 1000);
}
When I saw the question I instantly smiled and rubbed my hands ready to answer since I knew exactly the answer and they told me:
"Oh you looked already familiar to this one, we'll choose another for you"
I legit stood up and left the fucking interview right there.
rant
js
interview
web dev