2014-09-10 98 views
0

我想将我的Instagram中的图片显示到网站上。我成功地使用了图像显示部分,但是当我使用标记上传新图像时,我想在不刷新浏览器的情况下显示它。我想在1秒后再次调用代码。在特定的时间间隔后运行instagram代码

var feed = new Instafeed({ 
    clientId: 'fd49a37da9f04a47b046395a5d2a8fb9', 
    limit: 17 , 
    get:'tagged', 
    tagName:'kdy14', 
    after: function() { 
    var images = $("#instafeed").find('a'); 
    $.each(images, function(index, image) { 
     var delay = (index * 75) + 'ms'; 
     $(image).css('-webkit-animation-delay', delay); 
     $(image).css('-moz-animation-delay', delay); 
     $(image).css('-ms-animation-delay', delay); 
     $(image).css('-o-animation-delay', delay); 
     $(image).css('animation-delay', delay); 
     $(image).addClass('animated flipInX'); 
    }); 
    }, 
    template: '<a href="{{link}}" target="_blank"><img src="{{image}}" /></a>' 
}); 
feed.run(); 

我想让这个JS每1秒后运行一次。我怎么能这样。

回答

1

使用:

setInterval(function(){feed.run();}, 1000);//1000 is 1 s 

PS:如果你想停止它在某些地方这样做:

//first put the interval ID in an a var 
    var intervalID = setInterval(function(){feed.run();}, 1000); 

//then when you want to stop it just do 
    clearInterval(intervalID); 
0

setInterval另一种方法是一个递归setTimeout功能,有些人似乎宁愿:

var timer; 
(function looper() { 
    feed.run(); 
    timer = setTimeout(looper, 1000); 
}()); 

并予以清除:

clearTimeout(timer); 

DEMO