阵列图像中的JavaScript循环 [英] Array image looping in JavaScript

查看:117
本文介绍了阵列图像中的JavaScript循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在JavaScript中环路红绿灯的图像。我不知道该怎么做,有人可以给出建议。


解决方案

我已经3更改您的code


  1. 修正了typeo div.count dvi.count

  2. 修正缩进和大括号圆如果语句(并非绝对必要,反而使得code的方式更具可读性)

  3. 替换你讨厌使用字符串参数的的setTimeout 是一个函数引用


 函数changeimage(图像){
    VAR DVI =的document.getElementById(图像);
    如果(!dvi.count || dvi.count == image.length){
        dvi.count = 0;
    }
    dvi.src =图片[dvi.count]
    dvi.alt =图片[dvi.count]
    dvi.count = dvi.count + 1;
    超时= setTimeout的(函数(){
        changeimage(图像);
    },3500);
}


活生生的例子: https://jsfiddle.net/Lofug2hf/1/

I have been trying to loop traffic light images in JavaScript. I'm not sure what to do, can someone give advice.

解决方案

I have made 3 changes to your code

  1. Fixed the typeo div.count to dvi.count
  2. Corrected the indenting and braces round the if statement (Not strictly necessary, but makes the code way more readable)
  3. Replaced your nasty use of a string parameter in setTimeout to be a function reference


function changeimage(images){
    var dvi=document.getElementById(images);
    if(!dvi.count || dvi.count == image.length ){
        dvi.count=0;
    }
    dvi.src=image[dvi.count];
    dvi.alt=image[dvi.count];
    dvi.count=dvi.count+1;
    timeout=setTimeout(function(){
        changeimage(images);
    },3500);
}


Live example: https://jsfiddle.net/Lofug2hf/1/

这篇关于阵列图像中的JavaScript循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆