博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在jQuery中每5秒调用一次函数的最简单方法是什么? [重复]
阅读量:2380 次
发布时间:2019-05-10

本文共 1937 字,大约阅读时间需要 6 分钟。

本文翻译自:

This question already has an answer here: 这个问题已经在这里有了答案:

  • 11 answers 11个答案

JQuery, how to call a function every 5 seconds. jQuery,如何每5秒调用一次函数。

I'm looking for a way to automate the changing of images in a slideshow. 我正在寻找一种自动化幻灯片显示中图像更改的方法。

I'd rather not install any other 3rd party plugins if possible. 如果可能的话,我宁愿不安装其他任何第三方插件。


#1楼

参考:


#2楼

Just a little tip for the first answer. 只是第一个答案的小提示。 If your function is already defined, reference the function but don't call it!!! 如果您的函数已经定义,请引用该函数但不要调用它!!! So don't put any parentheses after the function name. 因此,请勿在函数名称后加上任何括号。 Just like: 就像:

my_function(){};setInterval(my_function,10000);

#3楼

You don't need jquery for this, in plain javascript, the following will work! 您不需要为此使用纯JavaScript的jquery,以下方法将起作用!

window.setInterval(function(){  /// call your function here}, 5000);

To stop the loop you can use 要停止循环,您可以使用

clearInterval()

#4楼

you could register an interval on the page using setInterval, ie: 您可以使用setInterval在页面上注册间隔,即:

setInterval(function(){     //code goes here that will be run every 5 seconds.    }, 5000);

#5楼

Both setInterval and setTimeout can work for you (

as @Doug Neiner and @John Boker wrote
both now point to setInterval ).
setIntervalsetTimeout都可以为您工作(
正如@Doug Neiner和@John Boker所写的
两者都指向setInterval )。
See for some more explanation about both to see which suites you most and how to stop each of them. 有关更多信息,请参见 ,以了解最适合您的套件以及如何停止每个套件。


#6楼

The functions mentioned above execute no matter if it has completed in previous invocation or not, this one runs after every x seconds once the execution is complete 上面提到的函数无论是否在先前的调用中完成都将执行,一旦执行完成,此函数将每隔x秒运行一次

// IIFE(function runForever(){  // Do something here  setTimeout(runForever, 5000)})()// Regular function with argumentsfunction someFunction(file, directory){  // Do something here  setTimeout(someFunction, 5000, file, directory)  // YES, setTimeout passes any extra args to  // function being called}

转载地址:http://fuexb.baihongyu.com/

你可能感兴趣的文章
开发平台软件中关于第三方库管理的一些思考
查看>>
svn创建分支的做法
查看>>
“当前不会命中断点。源代码与原始版本不同”的问题的有效解决办法
查看>>
对面向对象和面向过程的一些新理解
查看>>
软件开发中的资源管理
查看>>
gdal集成kml库的做法
查看>>
对类前置声明和包含头文件的一点理解
查看>>
DLL封装框架视图经验总结二
查看>>
重新找回心灵的感动
查看>>
在软件开发中用户应该扮演怎样的角色
查看>>
“调试器的协议与调试对象不兼容”错误的解决
查看>>
简介分布式计算系统的硬件架构
查看>>
图形图像显示研究(二)
查看>>
延庆石京龙滑雪记
查看>>
有关博客的一些断想
查看>>
Windows Server2008上安装VS2008出错及解决办法
查看>>
自定义安装MS Office Project2007会出错
查看>>
有关无法打开预编译头文件错误的思考
查看>>
在对话框上创建视图的方法总结
查看>>
关于桌面软件的开发语言和开发框架的思考
查看>>