简体   繁体   English

如何一次执行一个循环的语句(jQuery)?

[英]How to execute statements of a loop one at a time (jQuery)?

I want to make a program in which if we click on an image, different images keep appearing till the last one is reached. 我想制作一个程序,其中如果单击图像,则直到出现最后一个图像为止,都会一直出现不同的图像。 So, I used jQuery to call a function when the image is clicked, which starts a loop through all the images by changing the "src" attribute. 因此,单击图像时,我使用jQuery调用了一个函数,该函数通过更改“ src”属性来启动所有图像的循环。 All the images are numbered in order and then the loop uses those numbers to print out the png images. 所有图像均按顺序编号,然后循环使用这些编号打印出png图像。

Here is the HTML code: 这是HTML代码:

<img src="1.png" id="zwitch" height="50">

Here is the jQuery code: 这是jQuery代码:

$(document).ready(function(){
    $("#zwitch").click(function(){
        for(var i = 0; i < 11; i++){
        $("#zwitch").attr("src", i + ".png");
        }
    });
});

There are ten images in the folder "1.png, 2.png, 3.png, 4.png.....", There are no errors, I just wanted to know if there is a way to show all the images for a defined period of time. 文件夹“ 1.png,2.png,3.png,4.png .....”中有十张图像,没有错误,我只是想知道是否可以显示所有图像在定义的时间段内。

Try this, it runs from 1 to 10 with 1000ms delay between each image (1 second): 尝试此操作,它从1到10运行,每个图像之间有1000ms的延迟(1秒):

$(document).ready(function() {
  $("#zwitch").click(function() {
    var i = 1, counter = setInterval(function() {
      $("#zwitch").attr("src", i + ".png");
      if(i++ == 10) clearInterval(counter);
    }, 1000);
  });
});

You can try: 你可以试试:

$(document).ready(function(){
$("#zwitch").click(function(){
    for(var i = 0; i < 11; i++){
    setTimeout( $("#zwitch").attr("src", i + ".png"), 2000);
    }
});

}); });

This code will show your image for 2000 milisecond (2 seconds) 此代码将显示您的图片2000毫秒(2秒)

Now you can try This: 现在,您可以尝试以下操作:

$(document).ready(function(){
var counter = 0;
$("#zwitch").click(function(){

var id = setInterval(function(){
$("#zwitch").attr("src", counter + ".png");
counter++;
if ( counter > 10 )
    clearInterval( id );
}, 2000)
});
}

it could be better if u have 10 different img tags and display them accordingly. 如果您有10个不同的img标签并相应显示它们,那就更好了。 the first solution that strikes is to use setTimeOut function But it works abnormal inside for loop. 遇到的第一个解决方案是使用setTimeOut函数,但是它在for循环内部工作异常。

You can use setTimeout for show image for some time. 您可以将setTimeout用于显示图像一段时间。

$("#zwitch").click(function(){
    for(var i = 0; i < 11; i++){
       (function(i) {
          setTimeout(function() {
            $("#zwitch").attr("src", i + ".png");
          }, i * 5000);
       })(i);
    });
});

I would say something like this: 我会说这样的话:

$(document).ready(function(){
    var z = $("#zwitch");
    var i = 1;
    var id = null;
    function updateImage() {
        z.attr("src", "http://www.tuxpaint.org/stamps/stamps/symbols/math/" + (++i) + ".png");
        if (5 <= i) { clearInterval(id); }
    }
    z.click(function(){
        id = setInterval(updateImage, 1000);
    });
});

Demo here (don't forget to click the image): http://jsfiddle.net/v5QXe/ 演示在这里(不要忘记单击图像): http : //jsfiddle.net/v5QXe/

Note: only 5 images available, juste replace 5 by 10. 注意:仅提供5张图片,只需将5替换为10。

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

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