简体   繁体   English

javascript setTimeout 函数,有问题吗

[英]javascript setTimeout fuction,is something wrong with it

i use settimeout function, why it is not working.我使用 settimeout function,为什么它不起作用。 i just want to change the image after every 2 seconds.我只想每 2 秒更改一次图像。 but nothing is happenting it is showing only first image but it is not changing.但什么也没发生,它只显示第一张图像,但没有改变。

<script type="text/javascript">

        function parta(){
            document.getElementById("carimg").src="c1.img";
            window.setTimeout(partb,2000);
        }
        function partb(){
            document.getElementById("carimg").src="c2.jpg";
            window.setTimeout(partc,2000);
        }
        function partc(){
            document.getElementById("carimg").src="c3.jpg";
            window.setTimeout(parta,2000);
        }


</script>

You're declaring three functions over and over and never calling any of them.您一遍又一遍地声明三个函数,并且从不调用它们中的任何一个。

If you want to toggle between three images, put them in an array and go through the array one element at a time using setInterval :如果要在三个图像之间切换,请将它们放入一个数组中,并使用setInterval一次一个元素地通过数组 go :

var images = ['c1.img', 'c2.jpg', 'c3.jpg'];
var imgNum = 0;

function rotateImage() {
    document.getElementById("carimg").src= images[imgNum];
    imgNum = (imgNum + 1) % images.length;
}

rotateImage();
setInterval(rotateImage, 2000);

If you want to keep your code close to what you have right now, you only need to change a few things:如果你想让你的代码接近你现在所拥有的,你只需要改变一些事情:

function parta() {
  document.getElementById("carimg").src="c1.img";
  window.setTimeout(partb,2000);
}
function partb() {
  document.getElementById("carimg").src="c2.jpg";
  window.setTimeout(partc,2000);
}
function partc() {
  document.getElementById("carimg").src="c3.jpg";
  window.setTimeout(parta,2000);
}

parta();

Using while is not necessary in this case, since each function will call each other after 2 seconds.在这种情况下不需要使用while ,因为每个 function 将在 2 秒后相互调用。 However, you still need to get that "loop" started, which is why you need to call parta() .但是,您仍然需要启动“循环”,这就是您需要调用parta()的原因。 Also, it might make more sense to use setInterval in this case.此外,在这种情况下使用setInterval可能更有意义。

You can play with this code snippet on CodePen: https://codepen.io/yvesgurcan/pen/RwwOawO .您可以在 CodePen 上使用此代码片段: https://codepen.io/yvesgurcan/pen/RwwOawO

First of all, you don't need to use while and secondly, you missed parentheses of the functions.首先,您不需要使用 while,其次,您错过了函数的括号。

 function parta(){ document.getElementById("carimg").src="c1.jpg"; window.setTimeout(partb,2000); } function partb(){ document.getElementById("carimg").src="c2.jpg"; window.setTimeout(partc,2000); } function partc(){ document.getElementById("carimg").src="c3.jpg"; window.setTimeout(parta,2000); } parta();
 <img src="" id="carimg">

Try this one试试这个

let imageSrc = ['c1.img', 'c2.jpg', 'c3.jpg'];
let imgIndex;
setInterval(()=>{
    if(imgIndex == undefined) {
        imgIndex = 0;
    } else {
        if(imgIndex >= imageSrc.length - 1) {
            imgIndex = 0;
        } else {
            imgIndex++;
        }
    }
    document.getElementById("carimg").src = imageSrc[imgIndex];
}, 2000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM