$('#start') executes the function myFunction() and $('#stop') end it. How do I stop myFunction() from executing?
function myFunction() {
$(document).mousemove(function(e) {
$('#field').html(e.pageY)
});
}
$('#start').click(function() {
myFunction();
});
$('#stop').click(function() {
//stop myFunction
});
As Daniel pointed out, you actually want to unbind the event handler. You can use
unbindfor this:But this will also remove all other
mousemoveevent handlers, that might be attached by other plugins or similar (I mean, you attach to thedocumentelement not a “custom” element, so it can be that other JavaScript code also binds handlers to this element).To prevent this, you can use event namespaces. You would attach the listener with:
and unbind:
This would only remove your specific handler.