mirror of
				https://github.com/mickael-kerjean/filestash.git
				synced 2025-10-31 10:07:15 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			49 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /* eslint-disable */
 | |
| 
 | |
| export function debounce(func, wait, immediate) {
 | |
|     var timeout;
 | |
|     return function() {
 | |
|         var context = this, args = arguments;
 | |
|         var later = function() {
 | |
|             timeout = null;
 | |
|             if (!immediate) func.apply(context, args);
 | |
|         };
 | |
|         var callNow = immediate && !timeout;
 | |
|         clearTimeout(timeout);
 | |
|         timeout = setTimeout(later, wait);
 | |
|         if (callNow) func.apply(context, args);
 | |
|     };
 | |
| }
 | |
| 
 | |
| export function throttle(func, wait, options) {
 | |
|     var context, args, result;
 | |
|     var timeout = null;
 | |
|     var previous = 0;
 | |
|     if (!options) options = {};
 | |
|     var later = function() {
 | |
|         previous = options.leading === false ? 0 : Date.now();
 | |
|         timeout = null;
 | |
|         result = func.apply(context, args);
 | |
|         if (!timeout) context = args = null;
 | |
|     };
 | |
|     return function() {
 | |
|         var now = Date.now();
 | |
|         if (!previous && options.leading === false) previous = now;
 | |
|         var remaining = wait - (now - previous);
 | |
|         context = this;
 | |
|         args = arguments;
 | |
|         if (remaining <= 0 || remaining > wait) {
 | |
|             if (timeout) {
 | |
|                 clearTimeout(timeout);
 | |
|                 timeout = null;
 | |
|             }
 | |
|             previous = now;
 | |
|             result = func.apply(context, args);
 | |
|             if (!timeout) context = args = null;
 | |
|         } else if (!timeout && options.trailing !== false) {
 | |
|             timeout = setTimeout(later, remaining);
 | |
|         }
 | |
|         return result;
 | |
|     };
 | |
| }
 | 
