在jQuery中获取函数的名称

时间:2014-10-13 14:44:28

标签: jquery

我正在与jQuery合作。我有一个场景,我有一个回调方法/函数,由两个不同的函数/方法调用。

问题:我怎么知道我的回调方法/函数中哪两个函数/方法调用了我的回调方法/函数?因为我有一些变量需要根据调用回调的方法/函数来分配一些值。

示例代码

var One = "";
var Two = "";

function first(){
    var urlink = "https://192.168.150.3/api1?jsonpCallback=myCallback";
    $.ajax({
        type: "GET",
        url:  urlink,
        dataType:"jsonp",
        async: false,
    });
}

function second(){
    var urlink = "https://192.168.150.3/api2?jsonpCallback=myCallback";
    $.ajax({
        type: "GET",
        url:  urlink,
        dataType:"jsonp",
        async: false,
    });
}

myCallback = function(data){
        /* if the caller is first then
        alert('first') & set value of var One; 
        else alert("second") & set value of var Two;
        How to do this?*/
        }

据我所读herethere,没有解决办法,至少对我来说不起作用。任何建议/工作将不胜感激。 :)

2 个答案:

答案 0 :(得分:1)

尝试

$(function () {
    var one = "",
        two = "",
        _name = "",
        _url =  urlink; // without `?jsonpCallback=callback"`
    callback = function (data) {        
            $("#result")
            .append("<br>" + _name + ":" + data.result);
            alert(_name)
    };
    var cb = function (name) {
        $.ajax({
            beforeSend: function (jqxhr, settings) {
                _name = name
                if (_name === "first") {
                    one = _name
                } else {
                    two = _name
                }
            },
            type: "GET",
            url: _url,
            dataType: "jsonp",
            jsonpCallback: "callback"
        })
    };
    // call as `cb("first")` , `cb("second")
    // allow 1s between calls
    $.when(cb("first"), 
           setTimeout(function() {
               cb("second")
           },1000))
});

另见

Pass additional parameter to a JSONP callback

Can I make a jQuery JSONP request without adding the '?callback=' parameter in URL?

&#13;
&#13;
$(function () {
    var one = "",
        two = "",
        _name = "",
        _url =  "https://gist.githubusercontent.com/"
                + "anonymous/9a6997f09de9b68c59b2/"
                + "raw/f7d7b756005ad6d2b88cf0211f78a2990d7d2dc7/"
                + "content.json";
    callback = function (data) {        
            $("#result")
            .append("<br>" + _name + ":" + data.result);
            alert(_name)
    };
    var cb = function (name) {
        $.ajax({
            beforeSend: function (jqxhr, settings) {
                _name = name
                if (_name === "first") {
                    one = _name
                } else {
                    two = _name
                }
            },
            type: "GET",
            url: _url,
            dataType: "jsonp",
            jsonpCallback: "callback"
        })
    };
    $.when(cb("first"), 
           setTimeout(function() {
               cb("second")
           },1000))
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我已经更新了代码。看看这有助于你并告知。 fiddle

看到这对你有帮助。

var tryFun = function(obj) {
        var callerFunction =this.name;
        alert(obj +" calling function "+callerFunction);

    };

function first() {
    this.name="first";
        tryFun.call(this,"akash");
    }

function second() {
    this.name="second";
        tryFun.call(this,"akash");
    }

first();
second();