Thursday 19 July 2018

Quick help with timeout plssss

function f ( ) { console.log('starting loop') for ( let i = 0; i < 1000; i++) { for ( let j = 0; j < 100000; j++) {} } done(); } function done() { console.log('done..'); } function fail() { console.log('Function call taking more than 2 seconds') } // function call f(); // setting timeout for failure... should break out of f() setTimeout(fail, 500); In repl this prints out :Starting loop done.. => 1002 // no clue why this is here Function call taking more than 2 seconds So I am trying to learn how to use setTimeout and I wrote this example code. Here I am looping 1000x100000 times (apprx. 10 seconds) and I want to create a timeout such that if the function f() is taking more than 2 seconds, than I call the fail() method to indicate that f() 'timed out'. How can do i that?Thanks for the help!

Submitted July 19, 2018 at 08:56PM by laugh3x

No comments:

Post a Comment