javascript - 图像循环 - 重新循环

标签 javascript jquery

我有一个在一定范围内的特定时间随机的函数。我在重新循环函数或将计数器恢复为 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) 但输出错误并且加倍了附加图片。感谢您的帮助。

最佳答案

看看this fiddle 。这是没有图像的代码,它只是显示如何更改循环的迭代量。希望对您有帮助。

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);

关于javascript - 图像循环 - 重新循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24842379/

相关文章:

javascript - 无法在主页中使用自定义组件 - Typescript 错误

javascript - 如何让scan()的使用更加清晰

javascript - JQuery 数据表响应扩展 : column class not retained for cells in expanded rows

jQuery FormData - 如何设置字符编码?

javascript - 对象上下文中的变量

jquery - IE 无法识别 jQuery 选择器

javascript - 我想将 cmd 输出写入文件而不是 stdout

jquery - 如何使用 Jquery 和 Youtube api V3 获取 Youtube 评论?

jquery - 使用 jQuery 模拟单击​​选择元素

javascript - 为什么 node.js 是异步的?