views:

76

answers:

4

Hi everybody ,

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!

A: 

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

Starx
I could help you better, if you post your whole code including the function which runs the code in every 5 seconds
Starx
A: 

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?

griegs
+1  A: 

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;
    },
RobertPitt
Another option is using the .data option. if ($('#test').data('triggered')) type check.
halkeye
I never thought of that, silly me.. depending on weather the element #test is being updated via the ajax, then its going to have to go in live mode
RobertPitt
A: 

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>
nickf