如何停用铯中的 onTick 方法
How to deactivate onTick method in Cesium
我在使用 onTick()
方法时遇到问题,我有两个按钮用于两种不同的情况,在每种情况下我都会加载单独的数据源,就像 Sandcastle 上的 czml.html
示例一样。我对每个按钮分别有两个不同的 onTick()
方法定义,当我们在特定时间到达时做一些特定的事情。在重置方法中,我正在删除我的查看器的实体和数据源,但我无法删除 onTick
方法实现。
当我在 运行 我的代码中时,默认按钮运行良好,但是当我按下其他按钮时,我为第一个按钮提到的所有情况也会同时发生,并且它不会让我的第二个 onTick
方法正确执行其工作。你知道我如何停用第一个 onTick
方法吗?
看一下addEventListener documentation, you'll find two different ways to undo the addition of an event listener: One is to call the matching removeEventListener,另一个是保存addEventListener
的return值,以后再调用。
这里有一些代码显示了这两种方式。使用对您的代码更有意义的选项,但不要同时使用两者。
function onTickCallback(clock) {
// ... do stuff with every tick ...
}
var unsubscribeTicks = viewer.clock.onTick.addEventListener(onTickCallback);
function unsubscribeOption1() {
// One option is to call removeEventListener. In this case, you
// don't need to save "var unsubscribeTicks" at all, but you do
// need a reference to the original onTickCallback.
viewer.clock.onTick.removeEventListener(onTickCallback);
}
function unsubscribeOption2() {
// The other option is to save the return value of addEventListener,
// and later invoke it. This could be a cleaner option if the
// onTickCallback was declared anonymously inline.
unsubscribeTicks();
}
我在使用 onTick()
方法时遇到问题,我有两个按钮用于两种不同的情况,在每种情况下我都会加载单独的数据源,就像 Sandcastle 上的 czml.html
示例一样。我对每个按钮分别有两个不同的 onTick()
方法定义,当我们在特定时间到达时做一些特定的事情。在重置方法中,我正在删除我的查看器的实体和数据源,但我无法删除 onTick
方法实现。
当我在 运行 我的代码中时,默认按钮运行良好,但是当我按下其他按钮时,我为第一个按钮提到的所有情况也会同时发生,并且它不会让我的第二个 onTick
方法正确执行其工作。你知道我如何停用第一个 onTick
方法吗?
看一下addEventListener documentation, you'll find two different ways to undo the addition of an event listener: One is to call the matching removeEventListener,另一个是保存addEventListener
的return值,以后再调用。
这里有一些代码显示了这两种方式。使用对您的代码更有意义的选项,但不要同时使用两者。
function onTickCallback(clock) {
// ... do stuff with every tick ...
}
var unsubscribeTicks = viewer.clock.onTick.addEventListener(onTickCallback);
function unsubscribeOption1() {
// One option is to call removeEventListener. In this case, you
// don't need to save "var unsubscribeTicks" at all, but you do
// need a reference to the original onTickCallback.
viewer.clock.onTick.removeEventListener(onTickCallback);
}
function unsubscribeOption2() {
// The other option is to save the return value of addEventListener,
// and later invoke it. This could be a cleaner option if the
// onTickCallback was declared anonymously inline.
unsubscribeTicks();
}