停止 jquery 中的触发器

发布于 2024-09-05 02:25:46 字数 277 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(4

甜宝宝 2024-09-12 02:25:57

好吧,将 ajax 调用放在 $(document).ready(function() { }); 中,它只会在文档准备好后执行。

或者,当您执行 ajax 调用时,创建一个会话标志来表示脚本已被执行。

我的意思是

这是你的 ajax 调用页面

<?
if(!isset($_SESSION['scriptflag']) or $_SESSION['scriftflag']==false) {
             //Your action
             $_SESSION['scriptflag'] = true; 
}
?>

明白了

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

<?
if(!isset($_SESSION['scriptflag']) or $_SESSION['scriftflag']==false) {
             //Your action
             $_SESSION['scriptflag'] = true; 
}
?>

get the point

愁杀 2024-09-12 02:25:55

设置一个标志,告诉您之前是否执行过此触发。如果有,则不要调用点击事件。

您还可以在执行触发器后,从 #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?

挽清梦 2024-09-12 02:25:53

在函数外部添加一个全局变量来跟踪

var trigger_triggered = false;

ajax 调用中某处的状态

 success: function (msg) {
        ...
        if(trigger_triggered == false)
        {
            $('#test').trigger('click');
            trigger_triggered = true; //set it executed here
        }
        return false;
    },

add a global variable outside the function to track the states

var trigger_triggered = false;

Somewhere in your ajax call

 success: function (msg) {
        ...
        if(trigger_triggered == false)
        {
            $('#test').trigger('click');
            trigger_triggered = true; //set it executed here
        }
        return false;
    },
软糖 2024-09-12 02:25:52

jQuery 有一个内置的事件方法,只能触发一次: one()

$('#test').one('click', function() {
    // your regular click function
    // which you only want to run once

    // for example:
    alert('event fired');
});

$('#test').trigger('click');   // "event fired"
$('#test').trigger('click');   // <nothing>

jQuery has a built-in method for events which should only be fired once: one().

$('#test').one('click', function() {
    // your regular click function
    // which you only want to run once

    // for example:
    alert('event fired');
});

$('#test').trigger('click');   // "event fired"
$('#test').trigger('click');   // <nothing>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文