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 = (() => {
let lastTime = 0;
return () => {
const now = performance.now(); const now = performance.now();
if (now - lastTime < 20) { if (now - lastTime < 20) {
type = 'mouse'; type = 'mouse', callback(type), document.removeEventListener('mousemove', mousemoveHandler);
callback(type);
document.removeEventListener('mousemove', mousemoveHandler);
} }
lastTime = now; 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);
}); });
}; };
``` ```