diff --git a/app/src/constants.ts b/app/src/constants.ts index 20fb4af35..07659c535 100644 --- a/app/src/constants.ts +++ b/app/src/constants.ts @@ -89,6 +89,7 @@ export abstract class Constants { public static readonly TIMEOUT_INPUT = 256; public static readonly TIMEOUT_LOAD = 300; public static readonly TIMEOUT_TRANSITION = 300; + public static readonly TIMEOUT_COUNT = 1000; // id public static readonly HELP_PATH = { diff --git a/app/src/layout/status.ts b/app/src/layout/status.ts index c15837b4f..0dd34ece9 100644 --- a/app/src/layout/status.ts +++ b/app/src/layout/status.ts @@ -149,23 +149,27 @@ export const initStatus = (isWindow = false) => { }; let countRootId: string; +let countTimeout: number; export const countSelectWord = (range: Range, rootID?: string) => { /// #if !MOBILE if (document.getElementById("status").classList.contains("fn__none")) { return; } - const selectText = range.toString(); - if (selectText) { - fetchPost("/api/block/getContentWordCount", {"content": range.toString()}, (response) => { - renderStatusbarCounter(response.data); - }); - countRootId = ""; - } else if (rootID && rootID !== countRootId) { - countRootId = rootID; - fetchPost("/api/block/getTreeStat", {id: rootID}, (response) => { - renderStatusbarCounter(response.data); - }); - } + clearTimeout(countTimeout); + countTimeout = window.setTimeout(() => { + const selectText = range.toString(); + if (selectText) { + fetchPost("/api/block/getContentWordCount", {"content": range.toString()}, (response) => { + renderStatusbarCounter(response.data); + }); + countRootId = ""; + } else if (rootID && rootID !== countRootId) { + countRootId = rootID; + fetchPost("/api/block/getTreeStat", {id: rootID}, (response) => { + renderStatusbarCounter(response.data); + }); + } + }, Constants.TIMEOUT_COUNT); /// #endif }; @@ -174,26 +178,30 @@ export const countBlockWord = (ids: string[], rootID?: string, clearCache = fals if (document.getElementById("status").classList.contains("fn__none")) { return; } - if (clearCache) { - countRootId = ""; - } - if (ids.length > 0) { - fetchPost("/api/block/getBlocksWordCount", {ids}, (response) => { - renderStatusbarCounter(response.data); - }); - countRootId = ""; - } else if (rootID && rootID !== countRootId) { - countRootId = rootID; - fetchPost("/api/block/getTreeStat", {id: rootID}, (response) => { - renderStatusbarCounter(response.data); - }); - } + clearTimeout(countTimeout); + countTimeout = window.setTimeout(() => { + if (clearCache) { + countRootId = ""; + } + if (ids.length > 0) { + fetchPost("/api/block/getBlocksWordCount", {ids}, (response) => { + renderStatusbarCounter(response.data); + }); + countRootId = ""; + } else if (rootID && rootID !== countRootId) { + countRootId = rootID; + fetchPost("/api/block/getTreeStat", {id: rootID}, (response) => { + renderStatusbarCounter(response.data); + }); + } + }, Constants.TIMEOUT_COUNT); /// #endif }; export const clearCounter = () => { countRootId = ""; document.querySelector("#status .status__counter").innerHTML = ""; + clearTimeout(countTimeout); }; export const renderStatusbarCounter = (stat: {