javascript - setTimeout is not working -
i'm new in javascript , i'm familiar thread.sleep in java. far know, javascript uses settimeout similar thread.sleep.
i'm using phantomjs print thread:
function dothing(i){ settimeout(function(){ console.log(i); }, 100); } for(var i=1; i<20; i++){ dothing(i); } phantom.exit(); it prints nothing!!
can please let me know, what's wrong here? :(
help appreciated!!
edited:
i'm using java program calling phantomjs script.
try this
function dothing(i, last){ settimeout(function(){ console.log(i); if (last) phantom.exit(); }, 100 * i); } for(var i=1; i<20; i++){ dothing(i, >= 19); } there 2 fixes in code (in comparison origin):
- phantom.exit() must called after last operation finished (in revision resolved using flag 'last' set true last iteration);
- it's better call settimeout different timeout values, let them fire 1 one (not critical, still it's better).
Comments
Post a Comment