node.js - Running setTimeout and showing log in terminal -
i trying show the log using below code in terminal, showing 1 time, wrong in code ?
i created file time.js below code
settimeout(function() { console.log("settimeout: it's been 1 second!"); }, 1000);
and running code in terminal this
node time.js
the output given below
apples-macbook-pro:s3 apple$ node time.js settimeout: it's been 1 second! apples-macbook-pro:s3 apple$
while expecting settimeout: it's been 1 second! log every minute
you should use setinterval
method instead:
setinterval(function() { console.log("setinterval: it's been 1 second!"); }, 1000);
however, beware code dependent on event loopback , if have code occupies event loopback long, code may not execute every second ideally want to.