如果函数结果 X 次为 Z,则调用另一个函数
if function result is Z for X times, call another function
我有一个函数 foo()
在鼠标移动时触发,所以只要鼠标在移动,它就会一直被调用。
foo()
可以是任意数字。如果 foo()
结果为 100,我需要触发另一个函数 3 次。
我该怎么做?
document.addEventListener('mousemove', (e) => {
const whatINeed = foo(e)
// here we go: foo result is always a number
// if that number is 100 for 3 times
// fire another function
})
document.addEventListener('mousemove', (() =>
let count=0;
return (e) => {
const whatINeed = foo(e)
if(whatINeed == 100) {
if(++count == 3) {
//fire another function
}
}
};
})());
我有一个函数 foo()
在鼠标移动时触发,所以只要鼠标在移动,它就会一直被调用。
foo()
可以是任意数字。如果 foo()
结果为 100,我需要触发另一个函数 3 次。
我该怎么做?
document.addEventListener('mousemove', (e) => {
const whatINeed = foo(e)
// here we go: foo result is always a number
// if that number is 100 for 3 times
// fire another function
})
document.addEventListener('mousemove', (() =>
let count=0;
return (e) => {
const whatINeed = foo(e)
if(whatINeed == 100) {
if(++count == 3) {
//fire another function
}
}
};
})());