1 回答
TA贡献1872条经验 获得超3个赞
从你所描述的情况来看
如果用户输入太快
并看到在元素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();
});
添加回答
举报