如何在一定时间间隔后执行脚本

时间:2014-09-09 10:48:40

标签: javascript html

我使用了twitter小部件在简单的网页上显示我的时间线。但我真正想要实现的是,每当我从推特发推文时,我都希望在我创建的网页上看到它而不会刷新。所以我想做的是每隔3秒左右调用一次脚本。请让我知道怎么做。

这是我所做的html页面的代码。

<html>
<head>
<title>Twitter Feed Test</title>

</head>
<body>
<p>Testing how to get the the twitter feeds to webpage</p>
<a class="twitter-timeline" href="https://twitter.com/happyman_mayur" data-widget-id="509282432604844032">Tweets by @happyman_mayur</a>
<script>
!function(d,s,id){
var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';
if(!d.getElementById(id))
{
js=d.createElement(s);
js.id=id;js.src=p+"://platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js,fjs);}
}(document,"script","twitter-wjs");
</script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

可能有更好的,特定于Twitter的方式,但在Javascript中最简单的方法是使用setInterval函数。

setInterval(function () {
    // Your code here
}, 3000)

More info

答案 1 :(得分:0)

使用setInterval或setTimeout(递归);

您应该访问http://www.w3schools.com/jsref/met_win_setinterval.asp

但要注意清除在卸载时的clearInterval或clearTimeout