BUG fix: debounce

This commit is contained in:
KUN1007 2024-03-07 20:12:50 +08:00
parent 70def2d337
commit ad806c430d
No known key found for this signature in database
GPG key ID: 1845322DB3B9DDB2
5 changed files with 18 additions and 39 deletions

View file

@ -74,9 +74,8 @@ onBeforeMount(() => {
} }
}) })
const saveMarkdown = (editorMarkdown: string) => { const saveMarkdown = (editorMarkdown: string) =>
// Create a debounce function debounce(() => {
const debouncedUpdateContent = debounce(() => {
/** /**
* Editor is in edit mode * Editor is in edit mode
*/ */
@ -105,9 +104,6 @@ const saveMarkdown = (editorMarkdown: string) => {
replyRewrite.value.content = editorMarkdown replyRewrite.value.content = editorMarkdown
} }
}, 1007) }, 1007)
debouncedUpdateContent()
}
</script> </script>
<!-- MilkdownEditorWrapper.vue --> <!-- MilkdownEditorWrapper.vue -->

View file

@ -46,8 +46,7 @@ const handleInput = () => {
return return
} }
// Create a debounce handling function return debounce(() => {
const debouncedInput = debounce(() => {
/** /**
* Editor is in reply mode * Editor is in reply mode
*/ */
@ -63,8 +62,6 @@ const handleInput = () => {
rewriteTitle.value = topicTitle.value rewriteTitle.value = topicTitle.value
} }
}, 300) }, 300)
debouncedInput()
} }
</script> </script>

View file

@ -23,10 +23,6 @@ const debouncedSearch = debounce((inputValue: string) => {
} }
}, 300) }, 300)
const searchTopics = () => {
debouncedSearch(inputValue.value)
}
watch( watch(
() => search.value.keywords, () => search.value.keywords,
() => { () => {
@ -52,7 +48,7 @@ onMounted(() => {
class="input" class="input"
:placeholder="`${$tm('mainPage.header.search')}`" :placeholder="`${$tm('mainPage.header.search')}`"
@input="debouncedSearch(inputValue)" @input="debouncedSearch(inputValue)"
@keydown.enter="searchTopics" @keydown.enter="debouncedSearch(inputValue)"
/> />
</div> </div>
</template> </template>

View file

@ -1,20 +1,16 @@
/* export const debounce = <F extends (...args: any[]) => any>(
* Debounce function that takes a function and a delay time as parameters fn: F,
*/ time: number
export type DebouncedFunction<T extends (...args: any[]) => any> = ( ): ((...args: Parameters<F>) => void) => {
...args: Parameters<T> let timeoutID: NodeJS.Timeout | null = null
) => void
export function debounce<T extends (...args: any[]) => any>( return function (...args: Parameters<F>) {
func: T, if (timeoutID !== null) {
delay: number clearTimeout(timeoutID)
): DebouncedFunction<T> { }
let timeoutId: ReturnType<typeof setTimeout>
return (...args) => { timeoutID = setTimeout(() => {
clearTimeout(timeoutId) fn(...args)
timeoutId = setTimeout(() => { }, time)
func(...args)
}, delay)
} }
} }

View file

@ -22,17 +22,11 @@ const emits = defineEmits<{
const commentValue = ref('') const commentValue = ref('')
// Handle comment input // Handle comment input
const handleInputComment = () => { const handleInputComment = () =>
// Create a debounced processing function debounce(() => {
const debouncedUpdateContent = debounce(() => {
content.value = commentValue.value content.value = commentValue.value
}, 300) }, 300)
// Call the debounced processing function
// which will execute the update operation only once within the specified delay
debouncedUpdateContent()
}
// Check if the comment is valid // Check if the comment is valid
const isValidComment = () => { const isValidComment = () => {
// Warning if comment content is empty // Warning if comment content is empty