node.js - How to call a function each 2 min while webserver is running? -


my problem call function every 2 minutes while webserver running. server starts this:

app.listen(1309, function(){     console.log("server listening");     dosomething(); }); 

and function dosomething()

var dosomething = function(){     while(true) {         sleep.sleep(10);         console.log("hello"); //the original function called here code works :p     } }; 

so yes function prints every 10 seconds (10 sec 'cause testcase, don't want wait 2 min atm) after starting webserver can't receive requests. (tested console.log)

i tried without function , receives them. guess while loop blocks rest of sever. how can call function every 2 minutes (or 10 sec) while server running , without missen requests ?

you need use setinteval function:

const 2mins = 2 * 60 * 1000; var dosomething = function() {    setinterval(function() {      console.log("hello");    }, 2mins); } 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -