Make it shorter

This commit is contained in:
atomiks
2017-12-30 03:11:20 +11:00
committed by GitHub
parent f9afb5e0ee
commit 0fc5cfed99

View File

@ -12,26 +12,17 @@ where the user can switch between either input type at will.
```js ```js
const onUserInputChange = callback => { const onUserInputChange = callback => {
let type = 'mouse'; let type = 'mouse', lastTime = 0;
const mousemoveHandler = () => {
const mousemoveHandler = (() => { const now = performance.now();
let lastTime = 0; if (now - lastTime < 20) {
return () => { type = 'mouse', callback(type), document.removeEventListener('mousemove', mousemoveHandler);
const now = performance.now(); }
if (now - lastTime < 20) { lastTime = now;
type = 'mouse'; };
callback(type);
document.removeEventListener('mousemove', mousemoveHandler);
}
lastTime = now;
};
})();
document.addEventListener('touchstart', () => { document.addEventListener('touchstart', () => {
if (type === 'touch') return; if (type === 'touch') return;
type = 'touch'; type = 'touch', callback(type), document.addEventListener('mousemove', mousemoveHandler);
callback(type);
document.addEventListener('mousemove', mousemoveHandler);
}); });
}; };
``` ```