jQuery 倒计时插件和 AJAX
我正在使用 jQuery 倒计时插件 来实现倒计时并在计时器到期时调用 Web 服务。
问题是我在页面上使用 AJAX,并且必须在每个 AJAX 请求上重新设置倒计时,如下所示:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(SetupTimer);
/*Initial setup*/
$(document).ready(function() {
SetupTimer();
});
function SetupTimer() {
var serverTime = new Date();
var cutoffTime = new Date($("#<%= cutoffTime.ClientID %>").val());
serverTime.setHours(serverTime.getHours());
if (serverTime < cutoffTime) {
$('#timer').countdown('destroy'); /*should work, but doesn't*/
$('#timer').countdown({ until: cutoffTime, serverTime: serverTime, onExpiry: OrderingEnded, format: 'yowdHMS' });
}
else
OrderingEnded();
}
由于某种原因,这会在每个请求上创建倒计时的新实例,从而导致大量调用倒计时到期时的 Web 服务。
如何确保一次只有一个倒计时实例?
编辑
在文档中找到了这一点,但并不适合我
$('#timer').countdown('destroy');
I'm using jQuery Countdown plugin to implement a Countdown and call a webservice when timer expires.
The problem is that I'm using AJAX on the page, and have to re-setup the Countdown on every AJAX request like so:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(SetupTimer);
/*Initial setup*/
$(document).ready(function() {
SetupTimer();
});
function SetupTimer() {
var serverTime = new Date();
var cutoffTime = new Date($("#<%= cutoffTime.ClientID %>").val());
serverTime.setHours(serverTime.getHours());
if (serverTime < cutoffTime) {
$('#timer').countdown('destroy'); /*should work, but doesn't*/
$('#timer').countdown({ until: cutoffTime, serverTime: serverTime, onExpiry: OrderingEnded, format: 'yowdHMS' });
}
else
OrderingEnded();
}
This, for some reason, creates a new instance of the Countdown on ever request, resulting in numerous calls to Webservice when Countdown expires.
How do I make sure that only one instance of the Countdown exists at a time?
EDIT
found this in documentation, doesn't do it for me though
$('#timer').countdown('destroy');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,明白了,只需要调用
beginRequest
ok, figured it out, just needed to call the
on beginRequest
您可能需要
setInterval()
。 它允许您每 X 毫秒重复一次计时器。 您可以使用clearInterval()
来停止它。这里有一些阅读内容比较
setInterval( )
和setTimeout()
。另一种选择是仅在 AJAX 调用的回调函数中重新创建计时器。 这将确保只有在先前的 AJAX 调用完成后才启动新的计时器,例如:
You probably want
setInterval()
. It allows you to repeat the timer every X milliseconds. You can useclearInterval()
to stop it.Here is some reading comparing
setInterval()
andsetTimeout()
.Another option to only recreate the timer in the callback function of the AJAX call. This will ensure a new timer is only started once the previous AJAX call is complete, such as:
我有类似的问题。 我正在使用 web 服务使用 jquery.countdown.js 插件中的 serversync 函数从服务器获取当前时间。 我遇到一个问题
倒计时如何同步使用 jquery 使用“jquery.countdown.js”插件?
请您找到解决方案并告诉我,谢谢。
I have a similar type of problem. I am using web services to get the current time from the server using serversync function in jquery.countdown.js plugin. I have a facing a problem
How countdown get Synchronise with jquery using "jquery.countdown.js" plugin?
will you please find the solution and let me know thanks.