Reputation:
I have a node.js server and client. The client has a video.js player, progressbar.js and socket.io. I want progressbars to show buffer percentage of other users.
Here is piece of server source
function updateProgressBars() {
io.sockets.emit('updateProgressBars'); //Send data to everyone
}
socket.on('userProgressBarUpdate',
function(data) {
socket.broadcast.emit('changeLocalProgressBar', data); //Send data to everyone but not sender
});
And here is client
socket.on('updateProgressBars',
function() {
var bufferLevel = myPlayer.bufferedPercent();
var data = {
bl: bufferLevel,
n: name
}
socket.emit('userProgressBarUpdate', data); //Send data to server
});
changeLocalProgressBarLevel
is just changing progressbars on client side so dont worry about it.
How can I make updateProgressBars()
be called every second.
Upvotes: 13
Views: 31247
Reputation: 4899
using setInterval
is not the best choise because you should always clearInterval
. I prefer using recursion with bluebird promises:
function a()
{
if(smthCompleted)
dosmth();
else return Promise.delay(1000).then(() => a());
}
This way you have much better control of your code.
Upvotes: 7
Reputation: 608
You can use setInterval(function(){ ... }, timeMiliSeconds)
setInterval(function(){ console.log("hi")},1000) //logs hi every second
Upvotes: 14
Reputation: 1035
You can achieve this by using f.e. the "setInterval" function.
function doStuff() {
//do Stuff here
}
setInterval(doStuff, 1000); //time is in ms
And even better:
let myVar = setInterval(function(){ timer() }, 1000);
function timer() {
//do stuff here
}
function stopFunction() {
clearInterval(myVar);
}
Upvotes: 6