原始的写法,没有问题
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;
}