停止 jquery 中的触发器
我有一个 AJAX 调用,每 5 秒执行一次此调用,当调用“成功”时,我有一个触发器,
success: function (msg) {
...
$('#test').trigger('click');
return false;
},
...
但我只需第一次触发一次,而不是每 5 秒一次!
有人可以建议我如何停止这个触发器,或者也许使用其他东西来触发这个“点击”
谢谢!
I have an AJAX call, which is doing this call every 5 seconds and when the call "succeed " I have a trigger
success: function (msg) {
...
$('#test').trigger('click');
return false;
},
...
But i need to do this trigger just once , the first time, not every 5 second !
Can somebody suggest me how to stop this trigger, or maybe to use another stuff to trigger this "click "
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,将 ajax 调用放在
$(document).ready(function() { });
中,它只会在文档准备好后执行。或者,当您执行 ajax 调用时,创建一个会话标志来表示脚本已被执行。
我的意思是
这是你的 ajax 调用页面
明白了
Well, place the ajax call, in the
$(document).ready(function() { });
it will only executes once your document is ready.Or, when you do the ajax call, create a session flag to denote the script already being executed.
What I mean is
this is your ajax call page
get the point
设置一个标志,告诉您之前是否执行过此触发。如果有,则不要调用点击事件。
您还可以在执行触发器后,从 #test 中删除 click 事件,这样当您调用 trigger('click') 时什么也不会发生。
或者我错过了问题的重点?
Set a flag which tells you whether you have done this trigger before. If you have then don't call the click event.
You could also, once the trigger has been executed, remove the click event from #test so that when you call trigger('click') nothing happens.
Or am i missing the point of the question?
在函数外部添加一个全局变量来跟踪
ajax 调用中某处的状态
add a global variable outside the function to track the states
Somewhere in your ajax call
jQuery 有一个内置的事件方法,只能触发一次:
one()
。jQuery has a built-in method for events which should only be fired once:
one()
.