Throttle function execution in Sencha Touch


Sencha Touch includes some notable features. The function Ext.Function.createThrottled is one of them.

This function prevents multiple function execution. The interval on which the passed function is executed can defined additionally.

Ok, let's take a look into the code:
// The function to execute at a regular time interval.
var fn = function () {

console.log('fired');
};

// The intervall in milliseconds on which 
// the passed function is executed.
var intervall = 5000; // 5 seconds

var myFunc = Ext.Function.createThrottled(fn, intervall, this);

// Excecuted immediately
myFunc();

// No matter how often myFunc() is called,
// it is only executed once and after 5 seconds
myFunc();
myFunc();
myFunc();
myFunc();
myFunc();
myFunc();
Tip: The function can be assigned to a class variable that is initialized via a constructor or init().

0 Kommentare:

Post a Comment