问题描述:

I have an Iframe whose source changes every 5 seconds:

$(window).load(function() {

if (typeof $('.frame') != 'undefined' || null) {

$(".frame").ready(function() {

var locations = ['http://www.webstarts.com/support/2011/03/how-to-add-an-iframe-to-your-webstarts-website/', 'http://www.wix.com/app-market/html-iframe-embed/overview',

'http://codepen.io/jmelgoza/pen/jEaGYg',

'http://www.oddee.com/item_96986.aspx',

'http://www.imdb.com/title/tt2488496/',

'http://www.w3schools.com/tags/tag_iframe.asp'

];

var len = locations.length;

var iframe = $('.frame');

var i = 0;

setInterval(function() {

iframe.attr('src', locations[++i % len]);

}, 5000);

});

}

});

网友答案:

I ended up using something like this:

  setInterval(function () {
                iframe.attr('src', locations[++i % len]);
                iframe.fadeOut(300, function () {
                    iframe.fadeIn(300);
                });
网友答案:

Try this:

setInterval(function() {
    iframe.attr('src', locations[++i % len]);
    iframe.fadeOut(300, function() {
        iframe.fadeIn(300);
    });
}, 5000);
网友答案:

I can update this answer as more issues with the code become relevant, but the first thing that stands out is this line:

if (typeof $('.frame') != 'undefined' || null) {

I think what you're looking for here is this for your defensive programming technique:

if ($('.frame') != null && $('.frame').length > 0) {
相关阅读:
Top