gaoluyang
4 天以前 92230c9a97dc9ce9df3313d11d26999c04bb6b26
src/uni_modules/uni-easyinput/components/uni-easyinput/common.js
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,56 @@
/**
 * @desc å‡½æ•°é˜²æŠ–
 * @param func ç›®æ ‡å‡½æ•°
 * @param wait å»¶è¿Ÿæ‰§è¡Œæ¯«ç§’æ•°
 * @param immediate true - ç«‹å³æ‰§è¡Œï¼Œ false - å»¶è¿Ÿæ‰§è¡Œ
 */
export const debounce = function(func, wait = 1000, immediate = true) {
   let timer;
   console.log(1);
   return function() {
      console.log(123);
      let context = this,
         args = arguments;
      if (timer) clearTimeout(timer);
      if (immediate) {
         let callNow = !timer;
         timer = setTimeout(() => {
            timer = null;
         }, wait);
         if (callNow) func.apply(context, args);
      } else {
         timer = setTimeout(() => {
            func.apply(context, args);
         }, wait)
      }
   }
}
/**
 * @desc å‡½æ•°èŠ‚æµ
 * @param func å‡½æ•°
 * @param wait å»¶è¿Ÿæ‰§è¡Œæ¯«ç§’æ•°
 * @param type 1 ä½¿ç”¨è¡¨æ—¶é—´æˆ³ï¼Œåœ¨æ—¶é—´æ®µå¼€å§‹çš„æ—¶å€™è§¦å‘ 2 ä½¿ç”¨è¡¨å®šæ—¶å™¨ï¼Œåœ¨æ—¶é—´æ®µç»“束的时候触发
 */
export const throttle = (func, wait = 1000, type = 1) => {
   let previous = 0;
   let timeout;
   return function() {
      let context = this;
      let args = arguments;
      if (type === 1) {
         let now = Date.now();
         if (now - previous > wait) {
            func.apply(context, args);
            previous = now;
         }
      } else if (type === 2) {
         if (!timeout) {
            timeout = setTimeout(() => {
               timeout = null;
               func.apply(context, args)
            }, wait)
         }
      }
   }
}