我尝试我的aframe-sky-component每3秒更改一次他的图像,但是它不起作用。这是我到目前为止编码的内容:
<script type = "text/javascript">
function startTimer(){
setInterval(function(){ nextimage(); }, 3000);
function nextimage() {
var zahl = 0;
if (zahl == 0) {
$("#skyid").attr("src","#sky_2");
zahl ++;
}
if (zahl == 1) {
$("#skyid").attr("src","#sky_3");
zahl ++;
}
if (zahl == 2) {
$("#skyid").attr("src","#sky_4");
zahl ++;
}
if (zahl == 3) {
$("#skyid").attr("src","#sky_1");
zahl ++;
}
if (zahl == 4) {
zahl == 0;
}
}
}
</script>
我想我在帮助:D
时有一些逻辑错误答案 0 :(得分:2)
每次调用nextImage
时,zahl
都设置为0。
您可以将其移至外部范围:
function startTimer(){
setInterval(function(){ nextimage(); }, 3000);
var zahl = 0;
function nextimage() {
....
就像我做here一样。现在,它没有通过调用nextImage()
来清零,因此它的作用就像一个计数器。
我还认为更优雅的解决方案是使用颜色阵列:
var colors = ["blue", "red", "green", "yellow"]
function nextimage() {
$("#skyid").attr("color", colors[zahl])
zahl = (zahl < colors.length - 1) ? ++zahl : 0
//zahl is incremented until its reached the end of the array
}