javascript - Add code intelligence to rest api links in nodejs -
i have nodejs app listens rest call being made it. however, want add intelligence code.
for instance want execute function given call every 5 minutes. if calls rest link every second or minute want execute every 5 minutes. how achieve such flexibility ?
random calls nodejs server
http:// abc.com/pager/a called 100 times within 5 minutes should execute function once first call , must resume executing function after 5 minutes http:// abc.com/pager/b http:// abc.com/pager/c sample code
app.post('/pager/:room',function(req,res){ var room = req.params.room; if( room == 'a'){ console.log("room called"); //execute function once every 5 minutes room func1(room) } else if( room == 'b'){ console.log("room b called"); //execute function once every 5 minutes room b func1(room) } else if( room == 'c'){ console.log("room c called"); //execute function once every 5 minutes room c func1(room) } });
here's generic function returns function disables specified number of ms after has been called.
function tempdisableaftercall(fn, ms) { var timetoreset = 0; return function() { var currenttime = new date().gettime(); if (currenttime < timetoreset) { return; } else { timetoreset = currenttime + ms; fn.apply(this, arguments); } } } // function temporarily disabled function printnumber(num) { console.log(num) } // create modified function disabled after has been called var printnumberwait = tempdisableaftercall(printnumber, 5000); // call function every 500 ms, see log every 5 seconds setinterval(function() { printnumberwait(new date().gettime()); }, 500)
Comments
Post a Comment