Javascript 和 Famo.us:Engine.removeListener() 不起作用

Javascript and Famo.us: Engine.removeListener() not working

本文关键字:removeListener 不起作用 Engine Famo us Javascript      更新时间:2023-09-26

快速问题,我有一个简单的函数,它通过定义一个函数"animate"来创建一些表面并将它们动画化,以调用每个引擎预渲染。这看起来应该有,但是,在使用 Engine.removeListener 预呈现时删除此侦听器不起作用。

function _createCube(){
   //create of some surfaces and modifiers
   Engine.on('prerender',animate);

  surface.on('click',function(){
     _stopAnimation.call(this);
  }
}
function _stopAnimation(){
  Engine.removeListener('prerender',animate); 
}

它确实有效,但代码中可能存在错误。 以下示例代码显示了使用 removeListener 的简单工作示例

在这里工作 jsBin 示例 单击计数器以启动和停止侦听器。

  var mainContext = Engine.createContext();
  var surface = new Surface({ 
    content: 'Famo.us Count ',
    properties:{
      cursor: 'pointer'
    }
  });
  mainContext.add(surface);
  var counter = 0;
  function animate() {
    counter+=1;
    surface.setContent('Famo.us Count ' + counter);
  }
  function _create(){

    surface.on('click',function(){
      if (!surface.started) {
        Engine.on('prerender',animate);
        surface.started = true;
      } else {
        _stopAnimation.call(this);
        surface.started = false;
      }
    });
  }
  function _stopAnimation(){
    Engine.removeListener('prerender',animate); 
  }
  _create.call(this);