I created js function and now i want that js function to call itself only once, My code is
function view(str){
$.ajax({
type: "POST",
url: '<?php echo base_url()?>index.php/main/'+str+'/',
success: function(output_string){
//i want to call function from here only once like view(str);
}
});
}
. How can i do that ? Thanks in advance, Currently it is showing me infinte loop.
Use a flag variable
var myflag = false;
function view(str) {
$.ajax({
type : "POST",
url : '<?php echo base_url()?>index.php/main/' + str + '/',
success : function(output_string) {
if (!myflag) {
view(str);
}
myflag = true;
}
});
}
Try adding a parameter to the function that keeps track of the count:
function view(str, count) {
if (count > 0) {
return;
}
$.ajax({
type: "POST",
url: '<?php echo base_url()?>index.php/main/'+str+'/',
success: function(output_string) {
view(count + 1);
// i want to call function from here only once like view(str);
}
});
}
Then you would initially call view
like this:
view(str, 0);
You can pass a bool as a parameter on whether the function should call itself again:
function view(str, shouldCallSelf){
$.ajax({
type: "POST",
url: '<?php echo base_url()?>index.php/main/'+str+'/',
success: function(output_string){
if (shouldCallSelf)
view(output_string, false)
}
});
}
You should call it with true the first time. It will then call itself with false the second time, will not execute again.
you are looking for jquery one
. http://api.jquery.com/one/
fiddle http://jsfiddle.net/XKYeg/6/
<a href='#' id='lnk'>test</a>
$('#lnk').one('click', function view(str) {
$.ajax({
type: "POST",
url: '<?php echo base_url()?>index.php/main/' + str + '/',
success: function (output_string) {
i want to call
function from here only once like view(str);
}
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.