每3秒刷新一次功能

时间:2015-06-30 09:25:39

标签: javascript html5

function random_imglink() {
    var myimages = new Array()
        //specify random images below. You can have as many as you wish
    myimages[1] = "This is text1."
    myimages[2] = "This is text2."
    myimages[3] = "This is text3."
    myimages[4] = "This is text4."
    myimages[5] = "This is text5."
    myimages[6] = "This is text6."

    var ry = Math.floor(Math.random() * myimages.length)
    if (ry == 0)
        ry = 1
    document.write('<h1 class="comingsoon">' + myimages[ry] + '</h1>')
}
random_imglink()

如何使上述代码每3秒刷新一次?比如,输出每3秒刷新一次。感谢。

5 个答案:

答案 0 :(得分:1)

setInterval是您查询的简便方法。

简单示例:

setInterval(function(){ alert("Hello"); }, 3000);

答案 1 :(得分:1)

不要使用document.write。请改用document.innerHTML

  

write()方法主要用于测试:如果在HTML文档完全加载后使用它,它将删除所有现有的HTML。

当然还有setInterval,正如其他答案所暗示的那样。

答案 2 :(得分:0)

您可以使用window.setInterval(random_imglink,3000);

$("#tab-additional_information").hide();

$(document).ready(function(){
     $(".tabs").on('click', 'a', function(e){
         e.preventDefault();
         $('#tabs-container .entry-content').hide();
         $($(this).attr("href")).show();
     });
});

答案 3 :(得分:0)

您可以使用

function random_imglink() {
    var myimages = new Array()
        //specify random images below. You can have as many as you wish
    myimages[1] = "This is text1."
    myimages[2] = "This is text2."
    myimages[3] = "This is text3."
    myimages[4] = "This is text4."
    myimages[5] = "This is text5."
    myimages[6] = "This is text6."

    var ry = Math.floor(Math.random() * myimages.length)
    if (ry == 0)
        ry = 1
    document.body.innerHTML = '<h1 class="comingsoon">' + myimages[ry] + '</h1>';
}


window.setInterval(random_imglink, 3000);

但要小心,因为如果您的浏览器没有在时间间隔内呈现内容,您应该会看到不稳定和丢帧。更好的方法是使用requestAnimationFrame

  

Window.requestAnimationFrame()方法告诉浏览器你   希望执行动画并请求浏览器调用   指定在下次重绘前更新动画的函数。该   method将一个回调作为一个参数,在之前调用   重绘。

@objc protocol DataContextFacade{

func checkfetchFeedsWithNoSession()->NormalFeedViewModel?
}

答案 4 :(得分:0)

我创造了一个小提琴,我认为可以做你想要的。

我希望你喜欢猫。

http://jsfiddle.net/alexjamesbrown/9cu5L666/

注意 - 我正在设置div的内部html,而不是整个文档

function random_imglink() {

    var myimages = new Array()
    //specify random images below. You can have as many as you wish
    myimages[1] = "http://placekitten.com/g/100/400"
    myimages[2] = "http://placekitten.com/g/200/250"
    myimages[3] = "http://placekitten.com/g/300/234"
    myimages[4] = "http://placekitten.com/g/500/300"
    myimages[5] = "http://placekitten.com/g/100/400"
    myimages[6] = "http://placekitten.com/g/430/100"

    var ry = Math.floor(Math.random() * myimages.length)
    if (ry == 0)
        ry = 1;

    var div = document.getElementById('myContainer');
    div.innerHTML='<h1 class="comingsoon"><img src="' + myimages[ry] + '"</img></h1>'
}
setInterval(function(){ random_imglink(); }, 3000);