图像循环-重新循环

Image loop - reloop

本文关键字:循环 新循环 -重 图像      更新时间:2023-09-26

我有一个函数,在一个范围内的特定时间是随机的。我在重新循环函数或将计数器还原为1时遇到麻烦,因此它在范围后继续循环。

下面是我的代码:
var i = 1;
function imgLoop() {
    setTimeout(function () {
        var rand = makeUniqueRandom();
        j = rand;
        img4 = '<div id="deck" class="center" >'
        img4 += '<img class="img_deck" src="http://example.com/images/' + j + '.jpg" />';
        img4 += '</div">'
        $(img4).hide().appendTo("#img_brd").fadeIn('slow');
        i++;
        setTimeout(function () {
            if (i < 30) {
                $("#img_deck_border").remove().toArray();
                imgLoop();
            }
        }, 2000)
    }, 100)
}
imgLoop();

我尝试将参数传递给imgLoop()作为imgLoop(i)然后imgLoop(1),但输出错误并将图像的附加加倍。谢谢你的帮助。

看看这个小提琴。这是没有图像的代码,它只是展示了如何改变循环的迭代次数。希望对你有帮助。

var i = 1;
var amount = 30;
var isRunning = false;
function imgLoop() {
    isRunning = true;
    setTimeout(function () {
        // here should be your work with images
        $("#output").text($("#output").text() + " " + i);
        i++;
        setTimeout(function () {
            if (i < amount) {
                imgLoop();
            }
            else {
                isRunning = false;
                alert("I'm done");
            }
        }, 200)
    }, 100)
}
function addIterations() {
    amount += 10;
    if (!isRunning)
        imgLoop();
}
function start() {
    $("#div-start").remove();
    imgLoop();
}
$("#div-start").on("click", start);
$("#div-add").on("click", addIterations);