清除超时在键控 iFrame 上不起作用

clearTimeout not working on keyup iFrame

本文关键字:iFrame 不起作用 超时 清除      更新时间:2023-09-26

我知道可能已经有很多这样的问题了,但我浏览了其中的一些,我无法得到解决方案。我目前正在使用所见即所得的编辑器,当用户进行键控时,我需要保存。

我不希望它更新每个更改,但最多每两秒更新一次。你能告诉我为什么这段代码不起作用吗?

var updatetimer;
var body=$('#iframe').contents().find('body');
body.attr('contenteditable', true);
$(body).on('keyup', function() {
    clearTimeout(updatetimer);
    updatetimer = setTimeout(function(){
        savepagetext();
    },2000);
});

它确实有效,但每次键savepagetext()都会触发。如何正确防止这种情况?

我能够使用全局window.setTimeout()对象使用此代码解决它。感谢您的建议!

var iframeBody = $('body', $('#iframe')[0].contentWindow.document);
iframeBody.attr('contenteditable', true);
$(iframeBody).on('keyup', function(event) {
    clearTimeout(window.updatetimer)
    window.updatetimer = setTimeout(function(){
        savepagetext();
    },2000);
});