/**
 * 延迟
 * @param ms
 * @returns
 */
export function sleep(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
/***
 * 窗口resize事件封装
 */
const resizeObjs = reactive<{
  time: any;
  callbackList: Function[];
}>({
  time: -1,
  callbackList: [],
});
window.addEventListener(
  "resize",
  () => {
    if (resizeObjs.time >= 0) {
      clearTimeout(resizeObjs.time);
    }
    resizeObjs.time = setTimeout(() => {
      resizeObjs.time = -1;
      try {
        resizeObjs.callbackList.forEach((callback: Function) => {
          callback();
        });
      } catch (error) {
        console.error(error);
      }
    }, 600);
  },
  false,
);
/**
 * @param callback 添加窗口变化事件
 */
export function addResizeTimeOut(callback: Function) {
  resizeObjs.callbackList.push(callback);
}
/**
 * @param callback 移除窗口变化事件
 */
export function removeResizeTimeOut(callback: Function) {
  resizeObjs.callbackList = resizeObjs.callbackList.filter((_callback) => {
    return callback != _callback;
  });
}
