我尝试我的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
每次调用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
}