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.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -