June 8, 2010

to stop trigger in jquery

Question by AlexC

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!

Answer by RobertPitt

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;
    },

Answer by Starx

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!