Make it shorter
This commit is contained in:
@ -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);
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user