javascript - Periodically call node.js function every second -


i have node.js server , client. client has video.js player, progressbar.js , socket.io. want progressbars show buffer percentage of other users.

here piece of server source

function updateprogressbars() {   io.sockets.emit('updateprogressbars'); //send data }  socket.on('userprogressbarupdate',   function(data) {     socket.broadcast.emit('changelocalprogressbar', data); //send data not sender   }); 

and here client

socket.on('updateprogressbars', function() {   var bufferlevel = myplayer.bufferedpercent();   var data = {     bl: bufferlevel,     n: name   }    socket.emit('userprogressbarupdate', data); //send data server }); 

changelocalprogressbarlevel changing progressbars on client side dont worry it.

how can make updateprogressbars() called every second.

using setinterval not best choise because should clearinterval. prefer using recursion bluebird promises:

function a() {   if(smthcompleted)     dosmth();   else return promise.delay(1000).then(() => a()); } 

this way have better control of code.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -