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

how to execute script after certain interval of time

本文关键字:执行 脚本 时间      更新时间:2023-09-26

我使用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>

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

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

更多信息

带有setInterval或setTimeout(递归);

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

但是要小心在卸载时清除Interval o clearTimeout