if(this.timer !=null){ window.clearInterval(this.timer); this.timer = null; } if (layerId == '' || layerId == null || featureId == '' || featureId == null) { return; } let layers = viewer.scene.primitives._primitives; let that = this; layers.forEach(item => { if (item.id && item.id == layerId) { item.root.children.forEach(function (element) { if (element.content != null && element.content != undefined) { let featureNum = element.content.featuresLength; for (let i = 0; i < featureNum; i++) { var feature = element.content.getFeature(i); if (feature.getProperty("id") == featureId) { let flag = true; that.timer = window.setInterval(function () { if (flag) { feature.color = Cesium.Color.RED; } else { feature.color = Cesium.Color.WHITE; } flag = !flag; }, 1000); break; } } } }) } })
时间: 2024-04-22 18:25:19 浏览: 100
根据您提供的代码,我看到您正在尝试清除定时器和设置新的定时器。请注意以下几点:
1. 首先,确保在调用 `window.clearInterval` 之前检查 `this.timer` 的值是否为 `null`。这样可以避免尝试清除一个不存在的定时器。
2. 其次,在设置新的定时器之前,确保将 `this.timer` 设置为 `null`。这样可以确保在清除旧的定时器之前,没有新的定时器在运行。
3. 另外,请确保在设置新的定时器之前,已经找到了对应的要素(`feature`)。这样可以避免在没有要素的情况下设置定时器,从而导致错误。
根据您提供的代码,似乎已经考虑到了这些问题。如果您仍然遇到问题,请提供更多的上下文信息和详细描述,以便我能够更准确地帮助您解决问题。
相关问题
将这段代码写成vue3格式,startCountdown() { let count = 60; this.countdown = `${count}秒后重新获取`; this.isDisabled = true; this.timer = setInterval(() => { count--; if (count <= 0) { clearInterval(this.timer); this.timer = null; this.countdown = '获取验证码'; this.isDisabled = false; } else { this.countdown = `${count}秒后重新获取`; } }, 1000); }
<script>
import { ref } from 'vue';
export default {
setup() {
const countdown = ref('获取验证码');
const isDisabled = ref(false);
let timer = null;
const startCountdown = () => {
let count = 60;
countdown.value = `${count}秒后重新获取`;
isDisabled.value = true;
timer = setInterval(() => {
count--;
if (count <= 0) {
clearInterval(timer);
timer = null;
countdown.value = '获取验证码';
isDisabled.value = false;
} else {
countdown.value = `${count}秒后重新获取`;
}
}, 1000);
}
return {
countdown,
isDisabled,
startCountdown
}
}
}
</script>
阅读全文