I would finally like to reach some clarity on this aspect of programming, being basically self-taught. It's to do with passing variables around between functions.
With the code below, data.roomId is not recognized in the first function (obviously). I could place the function inside the joinRoom function to make it recognize data.roomId, but then how does the leaveRoom function recognize sendHeartbeat?
`io.on('connection', function (socket) {
function sendHeartbeat(){
setTimeout(sendHeartbeat, 8000);
socket.broadcast.to(data.roomId).emit('ping', { beat : 1 });
}
socket.on('joinRoom', function (data) {
socket.join(data.roomId)
setTimeout(sendHeartbeat, 8000);
});
socket.on('leaveRoom', function (data) {
socket.leave(data.roomId)
clearTimeout(sendHeartbeat)
});
});`
function(callback)- so it is in scope, therefore (as you put it) "recognized"