在再次调用之前,Uncall调用函数

时间:2022-02-04 19:20:58

Is there any way to uncall called function before it is called again?On success of ajax call, content is loaded and I call my function (right after html(data) ) because in other case it's not working. If I again load content, function is duplicating and eg. all my alerts show twice. (If I make 10 ajax calls I get 10 alerts instead of one).

有没有办法在再次调用之前取消调用函数?在ajax调用成功时,内容被加载并且我调用我的函数(在html(数据)之后),因为在其他情况下它不起作用。如果我再次加载内容,功能是重复的,例如。我的所有提醒都显示两次。 (如果我进行10次ajax调用,我会得到10个警报而不是一个警报)。

I've tried to to call function only once (after first ajax call) but then It work only with first ajax call and stop working after another calls.

我试图只调用一次函数(在第一个ajax调用之后)但是它只能用于第一个ajax调用并在另一个调用之后停止工作。

The only idea I have in this case is to uncall function before another call but I didn't found nothing.

我在这种情况下唯一的想法是在另一个电话之前取消功能,但我没有发现任何东西。

// If I don't call this function in ajax call below code doesn't work
// Ajax loaded content
function readydoc()
{
    $(document).on("change","#example1",function(){
    // SOME CODE
   }

   $(document).on("change","#example2",function(){
    // SOME CODE
   }

   $(document).on("change","#example3",function(){
    // SOME CODE
   }

   $(document).on("change","#example4",function(){
    // SOME CODE
   }
}


// MY AJAX CALL
function displayPhoto(photoid){
    $.ajax({
        url: "includes/displayphoto.php?photo_id="+photoid
    }).done(function(data) {    
        if (data == false)
        {
            alert ("ERROR");
        }
        else
        {
            $('#content').html(data);
            readydoc(); // Here I call my function
        }
    });
}

// HTML
<div id="content"> <!-- ALL AJAX STUFF GOES HERE <-->
   <div id="example1"></div> <!-- loaded by ajax <-->
   <div id="example2"></div> <!-- loaded by ajax <-->
   <div id="example3"></div> <!-- loaded by ajax <-->
   <div id="example4"></div> <!-- loaded by ajax <-->
</div>

2 个解决方案

#1


1  

As i mentioned in the comments, that is how your code should look like:

正如我在评论中提到的,这就是你的代码应该是这样的:

$(document).ready(function(){
   $(document).on("change","#example1",function(){
     // SOME CODE
   }
   $(document).on("change","#example2",function(){
     // SOME CODE
   }
   $(document).on("change","#example3",function(){
     // SOME CODE
   }
   $(document).on("change","#example4",function(){
     // SOME CODE
   } 
});

function readydoc()
{
   //SOME OTHER CODE YOU WANT TO TRIGGER AFTER EVERY AJAX CALL
}

// MY AJAX CALL
function displayPhoto(photoid){
    $.ajax({
        url: "includes/displayphoto.php?photo_id="+photoid
    }).done(function(data) {    
        if (data == false)
        {
            alert ("ERROR");
        }
        else
        {
            $('#content').html(data);
            readydoc(); // Here I call my function
        }
    });
}

#2


1  

Every time your ajax call finishes, you're executing readydoc which is attaching the event handlers. Each call will attach more and more event handlers. Instead, call your event attaching function once on page load:

每次你的ajax调用结束时,你都在执行附加事件处理程序的readydoc。每次通话都会附加越来越多的事件处理程序。相反,在页面加载时调用一次事件附加功能:

$(function() {
   //this will run once when the page has loaded
   $(document).on("change","#example1",function(){
    // SOME CODE
   }

   $(document).on("change","#example2",function(){
    // SOME CODE
   }

   $(document).on("change","#example3",function(){
    // SOME CODE
   }

   $(document).on("change","#example4",function(){
    // SOME CODE
   }
});

Remove the call to readydoc from within your ajax call - it's not needed. This will attach your event handlers only once, regardless of how many times the ajax call is run.

从你的ajax调用中删除对readydoc的调用 - 不需要它。这将仅为您的事件处理程序附加一次,无论运行ajax调用的次数如何。

#1


1  

As i mentioned in the comments, that is how your code should look like:

正如我在评论中提到的,这就是你的代码应该是这样的:

$(document).ready(function(){
   $(document).on("change","#example1",function(){
     // SOME CODE
   }
   $(document).on("change","#example2",function(){
     // SOME CODE
   }
   $(document).on("change","#example3",function(){
     // SOME CODE
   }
   $(document).on("change","#example4",function(){
     // SOME CODE
   } 
});

function readydoc()
{
   //SOME OTHER CODE YOU WANT TO TRIGGER AFTER EVERY AJAX CALL
}

// MY AJAX CALL
function displayPhoto(photoid){
    $.ajax({
        url: "includes/displayphoto.php?photo_id="+photoid
    }).done(function(data) {    
        if (data == false)
        {
            alert ("ERROR");
        }
        else
        {
            $('#content').html(data);
            readydoc(); // Here I call my function
        }
    });
}

#2


1  

Every time your ajax call finishes, you're executing readydoc which is attaching the event handlers. Each call will attach more and more event handlers. Instead, call your event attaching function once on page load:

每次你的ajax调用结束时,你都在执行附加事件处理程序的readydoc。每次通话都会附加越来越多的事件处理程序。相反,在页面加载时调用一次事件附加功能:

$(function() {
   //this will run once when the page has loaded
   $(document).on("change","#example1",function(){
    // SOME CODE
   }

   $(document).on("change","#example2",function(){
    // SOME CODE
   }

   $(document).on("change","#example3",function(){
    // SOME CODE
   }

   $(document).on("change","#example4",function(){
    // SOME CODE
   }
});

Remove the call to readydoc from within your ajax call - it's not needed. This will attach your event handlers only once, regardless of how many times the ajax call is run.

从你的ajax调用中删除对readydoc的调用 - 不需要它。这将仅为您的事件处理程序附加一次,无论运行ajax调用的次数如何。