1 回答

TA貢獻1872條經驗 獲得超4個贊
從你所描述的情況來看
如果用戶輸入太快
并看到在元素highlight()的每次更改時都會調用計算量大的函數input
editor.addEventListener('input', e => {
? highlight(editor); // <--
? e.preventDefault();
});
我建議取消該調用以突出顯示。
嘗試這樣的事情:
// Vanilla debounce: https://gist.github.com/peduarte/7ee475dd0fae1940f857582ecbb9dc5f
function debounce(func, wait = 100) {
? let timeout;
? return function(...args) {
? ? clearTimeout(timeout);
? ? timeout = setTimeout(() => {
? ? ? func.apply(this, args);
? ? }, wait);
? };
}
// ...
// adjust delay to find a balance between responsiveness and performance
const delay = 500;?
const runHighlight = () => highlight(editor);
const debouncedHighlight = debounce(runHighlight, delay);
editor.addEventListener('input', e => {
? e.preventDefault();
? debouncedHighlight();
});
添加回答
舉報