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
Post a Comment