使用requestAnimationFrame在setTimeout期间出现控制台错误

时间:2014-11-05 13:07:31

标签: javascript console requestanimationframe

我在mousewheel上有一个事件监听器。执行的脚本应执行以下操作:

  • +1或-1来自变量,具体取决于滚动方向
  • 不允许所述变量在最后一次更改后的2秒内更改
  • mousewheel事件侦听器上使用requestAnimationFrame,因此浏览器不会持续触发函数

我已经阅读了很多关于requestAnimationFrame的内容,但我承认我仍然认为我完全不了解它。

这是我的代码:

//request animation frame
window.requestAnimFrame = (function(){
return  window.requestAnimationFrame       ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame    ||
        function(callback){ window.setTimeout(callback, 1000/60); };
})();


//vars
var pane = 1;
var scrollPause = false;


//user scrolling
function scrolling(e){

  //return if scroll paused
  if (scrollPause) return;

  //scroll up
  if (e.wheelDelta < 0) {
    pane += 1;
    scrollPause = true;
  }

  //scroll down
  if (e.wheelDelta > 0) {
    pane -= 1;
    scrollPause = true;
  }

  //reset scrollPause
  setTimeout(function(){ scrollPause = false; },2000);

  //log pane number
  console.log(pane);
}


//scroll event listener
window.onmousewheel = function(e){ requestAnimFrame(scrolling(e)); }

现在这确实有效,但在setTimeout期间,控制台反复记录以下错误:

Uncaught TypeError: Failed to execute 'requestAnimationFrame' on 'Window': The callback provided as parameter 1 is not a function.

这是使用requestAnimationFrame的错误方法吗?有没有更有效的方法来做到这一点?为什么我会收到此错误?

1 个答案:

答案 0 :(得分:0)

我认为你应该接听这样的电话 -

window.onmousewheel = function(e) { requestAnimFrame(scrolling); }
相关问题