Javascript 等待发出AngularJS HTTP请求

Javascript 等待发出AngularJS HTTP请求,javascript,jquery,html,angularjs,Javascript,Jquery,Html,Angularjs,对于我的网站,我正在发出访问API的http请求。它在按键事件时触发。问题是它在每次连续按键时都会进行许多ajax调用,因此为了控制这一点,我必须添加一个小的超时,比如300ms。我已经进行了如下http调用,现在如何修改它 var req = { method: 'GET', url: 'url of api', headers: { "Key": "keyofapi", "Accept": "application/json"

对于我的网站,我正在发出访问API的http请求。它在按键事件时触发。问题是它在每次连续按键时都会进行许多ajax调用,因此为了控制这一点,我必须添加一个小的超时,比如300ms。我已经进行了如下http调用,现在如何修改它

var req = {
    method: 'GET',
    url: 'url of api',
    headers: {
        "Key": "keyofapi",
        "Accept": "application/json"
    },
};

$http(req)
    .success(function(data, status, headers, config) {
    })
    .error(function(data, status, headers, config) {
    });

将函数包装在$timeout函数中,并提及超时(p.S它以毫秒为单位取值)。不要忘记在控制器参数中注入$timeout。 试试这个

$timeout(function () {
        $http({
                method: 'POST', //No I18N
                url: 'your URL', //No I18N
                params: { 
                    parameters if needed
                }
            }).success(function (data, status, headers, config) {
                //Do the required
            });
        }, 2500);

将函数包装在$timeout函数中,并提及超时(p.S它以毫秒为单位取值)。不要忘记在控制器参数中注入$timeout。 试试这个

$timeout(function () {
        $http({
                method: 'POST', //No I18N
                url: 'your URL', //No I18N
                params: { 
                    parameters if needed
                }
            }).success(function (data, status, headers, config) {
                //Do the required
            });
        }, 2500);

将函数包装在$timeout函数中,并提及超时(p.S它以毫秒为单位取值)。不要忘记在控制器参数中注入$timeout。 试试这个

$timeout(function () {
        $http({
                method: 'POST', //No I18N
                url: 'your URL', //No I18N
                params: { 
                    parameters if needed
                }
            }).success(function (data, status, headers, config) {
                //Do the required
            });
        }, 2500);

将函数包装在$timeout函数中,并提及超时(p.S它以毫秒为单位取值)。不要忘记在控制器参数中注入$timeout。 试试这个

$timeout(function () {
        $http({
                method: 'POST', //No I18N
                url: 'your URL', //No I18N
                params: { 
                    parameters if needed
                }
            }).success(function (data, status, headers, config) {
                //Do the required
            });
        }, 2500);

我实际上建议做一个工厂或服务,如果你有活动的请求,并等待它

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
此选项的用法是:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

我没有测试这个代码。我不知道它是否有效。但我希望你能明白。实际上,我建议你做一个工厂或服务,看看你是否有积极的请求,然后等待它

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
此选项的用法是:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

我没有测试这个代码。我不知道它是否有效。但我希望你能明白。实际上,我建议你做一个工厂或服务,看看你是否有积极的请求,然后等待它

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
此选项的用法是:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

我没有测试这个代码。我不知道它是否有效。但我希望你能明白。实际上,我建议你做一个工厂或服务,看看你是否有积极的请求,然后等待它

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
此选项的用法是:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

我没有测试这个代码。我不知道它是否有效。但我希望你能明白。实际上,我建议你做一个工厂或服务,看看你是否有积极的请求,然后等待它

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
此选项的用法是:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

我实际上建议做一个工厂或服务,如果你有活动的请求,并等待它

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
此选项的用法是:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

我实际上建议做一个工厂或服务,如果你有活动的请求,并等待它

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
此选项的用法是:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);

我实际上建议做一个工厂或服务,如果你有活动的请求,并等待它

yourApp.factory('apiReqeust', ['$http', function($http) {
  // If you want to complete all requests that you sending,
  // make this varable an empty array and push every new request inside it.
  var waitingRequest = null; // Or quenuedRequests = [];
  var isWorking = false;
  // In my case I will not make an array, and will have only one request that will wait.
  // I mean the last attempted request.
  // This is perfect for an Autocomplete API

  function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
 function request(options, callback) {
    if (isWorking) {
      // If you are already running request at this time.
      // Replace waiting request with this one. (this one is newer request)
      waitingRequest = {options: options, callback: callback}
      // EDIT! I forgot about the return statement.
      // You need to stop, otherwise the request will be executed anyway.
      return;
    }
    isWorking = true;

    $http(options).success(function(data) {
      // When the request ends, tell that you are not working
      // So you can execute next request immediately
      isWorking = false;

      // execute callback with data parameter.
      callback(data);

      // If you have waiting request yet. execute it.
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);

        waitingRequest = null;
      }
    }).error(function(err) {
      isWorking = false;
      callback(null, err);
      if (waitingRequest) {
        request(waitingRequest.options, waitingRequest.callback);
      }
    });
  }
  return request;
});
此选项的用法是:

// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);
// Factory or controller or whatever that can depend on above factory
youApp.factory('another-whatever', ['apiRequest', function(apiRequest) {
  options = {'Some': 'HttpOptions'};
  apiRequest(options, function(data, err) {
    if (err) throw new Error(err);
    // And here you doing what you want with the data.
  });
}]);


你到底在问什么?你的问题不清楚。@Jan我想在发出请求时添加一个延迟,检查此链接如果正在运行,你可以禁用该函数,@SebastiánEspinosa我不明白,你的意思是什么,请描述一下?我的意思是类似于标志的东西,当函数运行时将标志设置为false,当准备再次运行时将其设置为true,然后你可以在if上使用这个标志,如果函数被触发,那么函数只会在它不运行的时候运行。你到底在问什么?你的问题不清楚。@Jan我想在发出请求时添加一个延迟,检查此链接如果正在运行,你可以禁用该函数,@SebastiánEspinosa我不明白,你的意思是什么,请描述一下?我的意思是类似于标志的东西,当函数运行时将标志设置为false,当准备再次运行时将其设置为true,然后你可以在if上使用这个标志,如果函数被触发,那么函数只会在它不运行的时候运行。你到底在问什么?你的问题不清楚。@Jan我想在发出请求时添加一个延迟,检查此链接如果正在运行,你可以禁用该函数,@SebastiánEspinosa我不明白,你的意思是什么,请描述一下?我的意思是类似于标志的东西,当函数运行时将标志设置为false,当准备再次运行时将其设置为true,然后你可以在if上使用这个标志,如果函数被触发,那么函数只会在它不运行的时候运行。你到底在问什么?你的问题不清楚。@Jan我想在发出请求时添加一个延迟,检查此链接如果正在运行,你可以禁用该函数,@SebastiánEspinosa我不明白,你的意思是什么,请描述一下?我的意思是类似于标志的东西,当函数运行时将标志设置为false,当准备再次运行时将其设置为true,然后你可以在函数被触发的if上使用该标志,这样函数只在未运行时运行。请参见注释中的“我的编辑”。我忘记了第17行的
return
语句。请参见注释中的编辑。我忘记了第17行的
return
语句。请参见注释中的编辑。我忘记了第17行的
return
语句。请参见注释中的编辑。我忘记了第17行的
return
语句。