如果函数结果是Z次X次,则调用另一个函数

时间:2017-05-11 00:58:17

标签: javascript function loops mousemove

我有一个在鼠标移动时触发的函数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
})

1 个答案:

答案 0 :(得分:1)

document.addEventListener('mousemove', (() =>
    let count=0;
    return (e) => {
        const whatINeed = foo(e)
        if(whatINeed == 100) {
            if(++count == 3) {
                //fire another function
            }
        }
    };
})());