如何使用jquery循环iframe?

M.R*_*M.R 7 iframe jquery loops settimeout setinterval

我有两个iframe.当页面加载时,iframe1在8秒后加载,我需要在无限循环中显示iframe2替换iframe1.

我尝试了以下代码并将超时设置为8秒和10秒,但iframe1在2秒内更改.

function preview() {
    $('#iframe1').hide();
    $('#iframe2').show();
    setTimeout(function() {
        $('#iframe2').hide();
        $('#iframe1').show();
    }, 8000);
};
setInterval(preview, 10000)
Run Code Online (Sandbox Code Playgroud)

以上也不能顺利加载.如何无缝显示/隐藏它们?

Bal*_*ran 2

您可以使用递归函数执行此操作并传递参数

$('#iframe2').hide();
animateInfinite('#iframe', 1)

function animateInfinite(str, last) {
    $(str + last).show();
    setTimeout(function () {
        $(str + last).hide();
        animateInfinite('#iframe', ((last == 1) ? 2 : 1));  
    }, 8000)    
}
Run Code Online (Sandbox Code Playgroud)

或者使用setinterval

var iframe = $('[id^=iframe]').hide();
iframe.eq(0).show();
setInterval(function () {
    iframe.toggle();
}, 1000);
Run Code Online (Sandbox Code Playgroud)

演示版