es6类中调用递归

原始的写法,没有问题 

 function tick() {
            viewer.scene.render();
            console.log(ppp)
            if (ppp !== false) Cesium.requestAnimationFrame(tick);
            oceansurfclass.currentTime = (viewer.clock.currentTime.secondsOfDay - 14437) / 300.0;
            // console.log(oceansurfclass);
        }
  tick();

es6---类中调用递归下面的写法报错,因为第二次调用时—this指向的不再是实例化对象,而为空了,故此时Cesium.requestAnimationFrame(_this.tick1)中_this.tick1会报错。

tick1() {
     var _this = this;
      viewer.scene.render();
      console.log(_this)
      if (_this.ppp !== false) Cesium.requestAnimationFrame(_this.tick1);
      _this.currentTime = (viewer.clock.currentTime.secondsOfDay - 14437) / 300.0;
      }

所以解决方法是利用bind将 this 传给了tick1()函数,这样一直调用时,this永远指向实例化对象

tick1() {
     var _this = this;
      viewer.scene.render();
      console.log(_this.ppp)
      if (_this.ppp !== false) Cesium.requestAnimationFrame(_this.tick1.bind(_this));
      _this.currentTime = (viewer.clock.currentTime.secondsOfDay - 14437) / 300.0;
      }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值