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

View file

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

View file

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

View file

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

View file

@ -22,17 +22,11 @@ const emits = defineEmits<{
const commentValue = ref('')
// Handle comment input
const handleInputComment = () => {
// Create a debounced processing function
const debouncedUpdateContent = debounce(() => {
const handleInputComment = () =>
debounce(() => {
content.value = commentValue.value
}, 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
const isValidComment = () => {
// Warning if comment content is empty