Reputation: 26054
I have this function in my codebase:
let touch = true;
function init() {
let firstMousemoveHasOccured = false;
$(window).on('mousemove.touchdetection', () => {
if (firstMousemoveHasOccured) {
touch = false;
$(window).off('mousemove.touchdetection touchend.touchdetection');
} else {
firstMousemoveHasOccured = true;
}
});
$(window).on('touchend.touchdetection', () => {
touch = true;
$(window).off('mousemove.touchdetection touchend.touchdetection');
});
}
The event mousemove.touchdetection
is not a standard event, so where is this coming from?
Upvotes: 1
Views: 26
Reputation: 55663
These are jQuery namespaced events
The first part of the event name mousemove
is the event that, when fired calls the callback. The second part touchdetection
is meaningless, except it allows you a mechanism to turn off a specific class or group of mousemove
events, easily.
$(document).off('mousemove'); //turns off all callbacks attached to the `mousemove` event.
$(document).off('mousemove.touchdetection'); //turns of all callbacks attached to the mousemove event that have been attached with the touchdetection namespace
The purpose of this, as you'll see from reading the API docs is to allow you to easily modify your listeners in your application without affecting listeners attached by third party code.
Upvotes: 2