Johannes Reiners
Johannes Reiners

Reputation: 678

Prepare a method call for execution

I want to delay a prepared method call (prepared= all parameters already set) for execution. Example:

I have a textfield with the following listener method:

var storedRequest = null;
function doAjaxRequest() {
    //if there is no request at this moment, do the request
    //otherwise: store the request and do nothing

} 
//will be executed after a request is done
function callbackAjaxRequestComplete() {
    //is storedRequest != null --> Execute that request (the last one)
}

So, is there a possiblity to store a PREPARED method call for execution?

Upvotes: 0

Views: 149

Answers (2)

Raynos
Raynos

Reputation: 169401

var preparedMethod = method.bind(null, param1, param2, param3 /*, ... etc */);

Function.prototype.bind[docs]

Upvotes: 4

Pointy
Pointy

Reputation: 413737

You can do something like this:

var preparedOperation = function() {
  return actualOperation(param1, param2, param3);
};

Then a call at any time to "preparedOperation" will be a call to your actual function.

The Functional.js library has some interesting support code for that sort of thing.

Upvotes: 3

Related Questions