mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-06 22:29:44 +08:00
@ -1,8 +1,15 @@
|
||||
(function(ionic) {
|
||||
|
||||
/**
|
||||
* Various utilities used throughout Ionic
|
||||
*
|
||||
* Some of these are adopted from underscore.js and backbone.js, both also MIT licensed.
|
||||
*/
|
||||
ionic.Utils = {
|
||||
|
||||
// Return a function that will be called with the given context
|
||||
/**
|
||||
* Return a function that will be called with the given context
|
||||
*/
|
||||
proxy: function(func, context) {
|
||||
var args = Array.prototype.slice.call(arguments, 2);
|
||||
return function() {
|
||||
@ -10,6 +17,41 @@
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Only call a function once in the given interval.
|
||||
*
|
||||
* @param func {Function} the function to call
|
||||
* @param wait {int} how long to wait before/after to allow function calls
|
||||
* @param immediate {boolean} whether to call immediately or after the wait interval
|
||||
*/
|
||||
debounce: function(func, wait, immediate) {
|
||||
var timeout, args, context, timestamp, result;
|
||||
return function() {
|
||||
context = this;
|
||||
args = arguments;
|
||||
timestamp = new Date();
|
||||
var later = function() {
|
||||
var last = (new Date()) - timestamp;
|
||||
if (last < wait) {
|
||||
timeout = setTimeout(later, wait - last);
|
||||
} else {
|
||||
timeout = null;
|
||||
if (!immediate) result = func.apply(context, args);
|
||||
}
|
||||
};
|
||||
var callNow = immediate && !timeout;
|
||||
if (!timeout) {
|
||||
timeout = setTimeout(later, wait);
|
||||
}
|
||||
if (callNow) result = func.apply(context, args);
|
||||
return result;
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* Throttle the given fun, only allowing it to be
|
||||
* called at most every `wait` ms.
|
||||
*/
|
||||
throttle: function(func, wait, options) {
|
||||
var context, args, result;
|
||||
var timeout = null;
|
||||
@ -94,5 +136,6 @@
|
||||
ionic.extend = ionic.Utils.extend;
|
||||
ionic.throttle = ionic.Utils.throttle;
|
||||
ionic.proxy = ionic.Utils.proxy;
|
||||
ionic.debounce = ionic.Utils.debounce;
|
||||
|
||||
})(window.ionic);
|
||||
|
||||
Reference in New Issue
Block a user