to stop trigger in jquery
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 ever开发者_Go百科y 5 second !
Can somebody suggest me how to stop this trigger, or maybe to use another stuff to trigger this "click "
Thanks!
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>
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;
},
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?
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
精彩评论