jQuery的/ Ajax调用与计时器 [英] Jquery/Ajax call with timer
问题描述
我在想,如果有人可以帮助我。我有一个PHP页面进行回声行从数据库中。我希望通过jQuery / AJAX每隔30秒调用它。但我也希望能够调用的页面,在任何时间,所以,如果我通过表单中添加一条记录,一旦表单提交我想通过打电话AJAX页面更新结果的时候了。任何人都可以点我在正确的方向,或者提供一些基本的code,所以我可以揣摩了这一点?还是很新的jQuery / AJAX。
I was wondering if someone could help me out. I have a php page that echos out rows from a database. I want to call it via jquery/ajax every 30 seconds. But I also want to be able to call the page at any time so that if I add a record via the form, once the form submits I want the page via called to ajax to update the results right away. Can anyone point me in the right direction or provide some base code so I can try to figure this out? Still very new to jquery/ajax.
推荐答案
如果你想在定时器中设置的东西,你可以使用JavaScript的的setTimeout
或的setInterval
方法:
If you want to set something on a timer, you can use JavaScript's setTimeout
or setInterval
methods:
setTimeout ( expression, timeout );
setInterval ( expression, interval );
其中, EX pression
是一个函数,暂停
和的间隔
以毫秒为单位的整数。 的setTimeout
运行定时器一次,运行 EX pression
一次,而setInterval的将运行 EX pression
每次的间隔
通过。
Where expression
is a function and timeout
and interval
are integers in milliseconds. setTimeout
runs the timer once and runs the expression
once whereas setInterval will run the expression
every time the interval
passes.
所以,在你的情况下,它会工作是这样的:
So in your case it would work something like this:
setInterval(function() {
//call $.ajax here
}, 5000); //5 seconds
至于阿贾克斯的推移,看到jQuery的 阿贾克斯()
方法。如果您运行的间隔,没有什么阻止你调用同一个阿贾克斯()
从其他地方在code。
As far as the Ajax goes, see jQuery's ajax()
method. If you run an interval, there is nothing stopping you from calling the same ajax()
from other places in your code.
这篇关于jQuery的/ Ajax调用与计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!