Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';javascript函数中的t句柄同步_Javascript_Overriding - Fatal编程技术网

Can';javascript函数中的t句柄同步

Can';javascript函数中的t句柄同步,javascript,overriding,Javascript,Overriding,我正在重写如下javascript函数: (function() { origFunc = aFunction; aFunction = function() { doRequest(); //return origFunc.apply(this); }; })(); 我知道我需要以“return origFunc.apply(this)”结束函数,以便执行原始函数。但是,在执行请求时,我必须等待请求完成。

我正在重写如下javascript函数:

(function() {
    origFunc = aFunction;
    aFunction = function() {
        doRequest();                
        //return origFunc.apply(this);
    };
})();
我知道我需要以“return origFunc.apply(this)”结束函数,以便执行原始函数。但是,在执行请求时,我必须等待请求完成。这就是我编写此函数的原因:

doRequest: function()
{
    try
    {
        if(window.XMLHttpRequest)
            httpRequest = new XMLHttpRequest();
        else if(window.ActiveXObject)
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

        var url = anUrl, self = this;

        httpRequest.onreadystatechange = function(data)
        {
            try
            {
                if(httpRequest.readyState == 4)
                {
                    if(httpRequest.status == 200)
                       return origFunc.apply(self);
                    else if(httpRequest.status != 0 )
                       alert("Error while executing the request : "+httpRequest.status+"\r\nUrl : "+url);
                }
            }
            catch(e)
            {                    
            }
        };

        httpRequest.open("GET", url);
        httpRequest.send();
    }
    catch(err)
    {
        alert("Error : "+err);
    }        
}
你可以猜到,问题是我不能做那样的事情。
你知道我该怎么做吗?

下面是一个如何处理包装异步函数的示例

// This simply calls the callback with some data after a second
// Could be an AJAX call for example
var doSomethingAsync = function (callback) {
  setTimeout(function () {
    callback({ some: 'data' });
  }, 1000);
};

var fnThatMakesAsyncCall = function () {
  // From the outside there is no way to change this callback
  // But what if we need to intercept the async function to change the data given to it, or monitor it?
  // Then we'd have to wrap the async function to wrap the callback.
  var callback = function (data) {
    console.log('Original', data); 
  };

  doSomethingAsync(callback);
};

// Function to wrap another function and return the wrapper
var wrapFn = function (fn) {
  // Create the wrapped function.
  // Notice how it has the same signature with `callback` as the first argument
  var wrapped = function (callback) {
    // Here we get the original callback passed in
    // We will instead wrap that too and call the original function with our new callback
    var newCb = function (data) {
      // This will run once the async call is complete
      // We will as an example mutate the data in the return data of the callback
      data.some = 'Wrapped it';
      // Call the original callback with the changed data
      callback.call(this, data);
    };
    // Run the function we wrap with the new callback we supply
    fn.call(this, newCb);
  };
  // Return wrapped function
  return wrapped;
};

// Will log Original {some: "data"} 
fnThatMakesAsyncCall();
doSomethingAsync = wrapFn(doSomethingAsync);
// Will log Original {some: "Wrapped it"} 
fnThatMakesAsyncCall();

我不完全确定你的问题出在这里。您能更好地解释一下吗?总之,我希望在请求成功完成后执行原始函数。所以我不能像我在问题的第一部分所说的那样去做。是什么阻止你去做呢。这绝对是可能的,那么你在实现这一点上有什么具体问题呢?我不知道怎么做。如果我像问题的第一部分那样执行,即使请求没有完成,也会执行原始函数。由于函数是异步运行的,
doRequest
函数必须将回调作为参数。您可以包装该回调,然后调用原始回调,与第一个示例类似。就这些。