根据传入时间节流

28 阅读1分钟
export const handleExport =(function(){
  let isThrottled = false;
  let previousThrottleTime;
  return async (params, apiUrl, throttleTime) => {
    if (previousThrottleTime !== throttleTime) {
      isThrottled = false;
      previousThrottleTime = throttleTime;
    }
    // console.log(previousThrottleTime,throttleTime,isThrottled)
    if (!isThrottled) {
      isThrottled = true;
      ElMessageBox.confirm('确认要导出吗?', '温馨提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
        .then(async () => {
          const queryString = Object.keys(params)
            .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
            .join('&');
          const loading = ElLoading.service({
            lock: true,
            text: 'Loading',
            background: 'rgba(0, 0, 0, 0.7)',
          })
          const urlex = import.meta.env.VITE_API_BASEPATH+apiUrl+'?'+queryString;
          window.location.href=urlex
          loading.close()
        })
        .catch(() => {
          ElMessage({ type: 'info', message: '取消导出' });
          isThrottled = false
        });
      setTimeout(() => {(isThrottled = false)}, throttleTime);
    }
  }
})();