Ted Tzuridis Ted Tzuridis - 2 months ago 5
Node.js Question

Why isn't my reset function stopping?

My reset button continues to clear the canvas and empty the chat box. Shouldn't the return statement end the function?

Main.js

var reset = function() {
context.clearRect(0,0, canvas[0].width, canvas[0].height);
context.beginPath();
$('#guesses').children().empty();
socket.emit('reset', reset);
return;
};


$('#reset').on("click", reset);

socket.on('reset', reset);


Server.js

socket.on('reset', function(){
socket.broadcast.emit('reset');
});

Answer

At the end of your reset function, you're emitting a reset event.

socket.emit('reset', reset);

You've also setup a handler for when the reset event is emitted.

socket.on('reset', reset);

So every time you call reset, you're emitting an event which ends up calling reset again.

Comments