简体   繁体   English

尝试回叫功能时我做错了

[英]What I'm doing wrong when trying call back function

I am trying to use a call back function for this function in javascript 我正在尝试为此函数在JavaScript中使用回调函数

function Filtering_GetSite(siteElement) {

$.ajax({

    type: "POST",
    url: "samle.asmx/f1",
    data: "",
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function (response) {

        var filtersitedetails = response.d;
        var appendItem = "";

        $(siteElement).empty();

        $.each(filtersitedetails, function (index, Filtering_GetSiteInfo) {

            var activeappend = "";
            var id = Filtering_GetSiteInfo.id;
            var site = Filtering_GetSiteInfo.site;

            activeappend = "<option value=" + id + ">" + site + "</option>";

            appendItem += activeappend;

        });

        $(siteElement).prepend('<option disabled="disabled" selected="selected" value="">Select Site</option>');
        $(siteElement).append(appendItem);

    },

    error: function (response) {
        alert("error in Filtering_GetSite");
    }
});
}

and this is how I call the function and trying to use call back function 这就是我调用该函数并尝试使用回调函数的方式

Filtering_GetSite("#txt-site-name", function () {
           alert('ok')
           Dashboard_GetAgentInfo(agentParam,agentElement);
       });

but the problem is it doesn't go to the alert when the function is finished. 但是问题是,功能完成后,它并没有进入警报状态。 How can I do this? 我怎样才能做到这一点?

That is because you do not seem to pass and invoke that method when the ajax invokes successfully. 那是因为当ajax成功调用时,您似乎并没有传递和调用该方法。

First you will have to change the method signature of Filtering_GetSite to accept a 2nd parameter which in this case is the callback and then when the ajax is a success you just invoke it. 首先,您必须更改Filtering_GetSite的方法签名以接受第二个参数,在这种情况下,该参数是callback ,然后,当ajax成功执行时,您只需调用它即可。

function Filtering_GetSite(siteElement, callback) {
   ...

   success: function() {
      ....

      callback();
   }

   ...
}

You haven't set your callback parameter into your Filtering_GetSite function. 您尚未在Filtering_GetSite函数中设置回调参数。 eg 例如

function Filtering_GetSite(siteElement, callback) {
}

You can then call the callback with callback() where you want it to fire. 然后,可以在希望触发的地方使用callback()调用该回调。 For example within the success block; 例如在success块内;

This is a wrap. 这是一个包裹。

You need to specificy a second parameter for a callback function 您需要为回调函数指定第二个参数

I also added a condition so when you omit the second parameter it will check the callback if its a function or not so you can call the function just Filtering_GetSite(sample) insted of Filtering_GetSite(sample,function(){}) 我还添加了一个条件,以便当您省略第二个参数时,它将检查callback是否是函数,因此您可以仅在由Filtering_GetSite(sample,function(){})插入的Filtering_GetSite(sample)调用该函数。

function Filtering_GetSite(siteElement, callback) {
 if (typeof (callBack) == "function") {
            callBack();
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试更新我的产品时,我做错了什么? - What am I doing wrong, when I'm trying to update my products? 尝试增加数字变量时的角度“此未定义”,不确定我在做什么 - Angular “this is undefined” when trying to increment number variable, not sure what I'm doing wrong 尝试在pythoncef中调用JS时我做错了什么? - What am I doing wrong while trying to call JS in pythoncef? 我正在尝试通过输入生日来输出年龄的函数。 我没有从中得到输出。 我做错了什么? - I'm trying to make a function that outputs age by entering birthdate. I'm not getting an output from this. What I'm I doing wrong? 将Python代码实现为Javascript时,我在做什么错? - What i'm doing wrong when implementing Python code to Javascript? 在满足一定的分数长度后,我试图阻止我的 function 显示元素。 我究竟做错了什么? - I'm trying to stop my function from displaying elements after a certain score length is satisfied. What am I doing wrong? 当单击按钮时,我试图将输入的内容保存到本地存储中。 我究竟做错了什么? - I'm trying to save the content of an input into local storage when a button is clicked. What am I doing wrong? AngularJS-我在注射中做错什么? - Angularjs - what I'm doing wrong with injections? 我试图一键挂起 2 个功能,然后出现错误,我做错了什么? - I'm trying to hang 2 functions on one click, and getting an error, what am I doing wrong? 我究竟做错了什么? 我正在尝试通过jQuery单击来放大和缩小图像 - What am I doing wrong? I'm trying to make an image bigger and smaller on a click with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM