
/**
 * 函数防抖
 * 触发事件后在n秒后执行，如果n秒内又触发事件，则重新计算时间
 */
export function debounce(fn, wait = 1000) {
  let timer;
  return function () {
    let context = this;
    let args = arguments;
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(context, args);
    }, wait)
  }
}

/**
 * 函数节流
 * 触发事件立即执行，但在n秒内连续触发则不执行
 */
export function throttle(fn, wait = 1000) {
  let timer;
  return function () {
    if (timer != null) return;
    let context = this;
    let args = arguments;
    fn.apply(context, args);
    timer = setTimeout(() => {
      timer = null;
    }, wait);
  }
}

export function msg(message,dur){
  uni.showToast({
    icon: 'none',
    title: message,
    duration: dur || 1500
  })
}
export function success(message,dur){
  uni.showToast({
    icon: 'success',
    title: message,
    duration: dur || 1500
  })
}