JavaScript 间隔

发布于 2024-10-07 23:55:47 字数 364 浏览 0 评论 0原文

js中如何使用区间?例如我想每5秒调用一个函数?

<script type="text/javascript">

setInterval(openAPage(), 5000);

function openAPage() {
var startTime = new Date().getTime();
var myWin = window.open("http://www.sabah.com.tr","_blank")
var endTime = new Date().getTime();
var timeTaken = endTime-startTime;
</script>

这个脚本不起作用,有人知道为什么吗?

How can I use interval in js? For example I want to call a function every 5 seconds?

<script type="text/javascript">

setInterval(openAPage(), 5000);

function openAPage() {
var startTime = new Date().getTime();
var myWin = window.open("http://www.sabah.com.tr","_blank")
var endTime = new Date().getTime();
var timeTaken = endTime-startTime;
</script>

This script doesn't work, anyone know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

城歌 2024-10-14 23:55:47

这些答案很全面、很好;我只是想专门修复你的问题。有关如何/为什么的信息,请参阅其他答案。

setInterval(openAPage, 5000);

请注意缺少 ()

此外,您还缺少 openAPage() 函数的结束 }

These answers are thorough and good; I just want to specifically fix yours. See the other answers for HOW/WHY.

setInterval(openAPage, 5000);

Note the lack of ().

Also, you're missing the closing } on the openAPage() function.

千里故人稀 2024-10-14 23:55:47
setInterval(function(){
  /* your code here */
}, 5000);

如果需要将数据传递给函数,可以使用闭包来完成:

setInterval(function(param){
  return function(){
    console.log(param);
  };
}("hello"), 5000);

将在控制台打印“hello”。

setInterval(function(){
  /* your code here */
}, 5000);

And if you need to pass data to the function, you can do it with a closure:

setInterval(function(param){
  return function(){
    console.log(param);
  };
}("hello"), 5000);

will print "hello" to the console.

千纸鹤 2024-10-14 23:55:47
setInterval(functionName, 5000)
setInterval(functionName, 5000)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文