【问题标题】:jQuery/javascript: webkit clearTimeout issuejQuery/javascript:webkit clearTimeout 问题
【发布时间】:2012-03-31 19:25:38
【问题描述】:

在我上一个问题的后面,我在 webkit 浏览器 Safari 和 Chrome 中的以下代码遇到了一些问题:-

// Textarea focus out event.
var renderHandler;


$("textarea").live('focusout', function (e) {
   var currentNote = this;
   renderHandler = setTimeout( function(){ renderNote(currentNote); }, 100);
});

// handle exceptions
$('.textEdit').live('focusin', function (e) {
   clearTimeout(renderHandler);

});



function renderNote( note ){
var itemContent = $(note).val();
    itemContent = htmlStrip(itemContent);
    itemContent = itemContent.replace(/\n/g, "<br/>"); // New lines
    //itemContent = itemContent.replace(/\s/g, " &nbsp;"); // Spaces

// Formatting replacements
itemContent = itemContent
    .replace(/\[b\]/gi, "<b>")
    .replace(/\[\/b\]/gi, "</b>")
    .replace(/\[i\]/gi, "<i>")
    .replace(/\[\/i\]/gi, "</i>")
    .replace(/\[s\]/gi, "<s>")
    .replace(/\[\/s\]/gi, "</s>");

$(note).replaceWith("<p class='notes'>"+ itemContent +"</p>");
}

在 firefox 中,renderHandler 上的最新 clearTimeout 阻止了函数“renderNote”被调用,这允许我处理 focusout 事件的异常。然而,在 webkit 浏览器中,无论如何都会调用 renderNote。

我尝试过 return、return false、preventDefault、stopPropagation、break 但没有任何乐趣。有人遇到过这种情况吗?

这是一个链接:http://www.kryptonite-dove.com/sandbox/animate

如果您双击正文,然后单击注释的正文,您可以看到它的运行情况。

【问题讨论】:

  • 工作小提琴演示:jsfiddle.net/userdude/TsuZn
  • 其实我看到上面的代码是functionality.js的一部分。试试看:jsfiddle.net/userdude/TsuZn/1
  • 相同的差异伙伴,他们都展示了一个工作示例:)
  • 是的,但是第一个不能由感兴趣的 SOer 编辑,因为它包含在 .ready() 块中。
  • 如果有人单击其中一种文本样式(删除线、粗体、斜体),您是否正在尝试阻止渲染?

标签: javascript jquery google-chrome safari webkit


【解决方案1】:

注意,$.live() 已弃用;应该改用$.on()$.delegate()。出于某种原因,我无法正确使用其中任何一个,因此我无法建议如何执行此操作,但您应该考虑避免使用$.live(),因为它最终会被删除,并且是一个性能问题.

据我所知,这行:

$('.textEdit').live('focusin', function (e) {

从不在 Chrome 中运行。没关系,因为它看起来像

$('.textEdit').live('click', function (e) {

应该可以正常工作。

我会修改您的方法并使用块变量,而不是取消超时。例如:

var renderHandler,
    blockRender = false;

$("textarea").live('focusout', function (e) {
    var currentNote = this;
    renderHandler = setTimeout(function(){ 
        renderNote(currentNote); 
    }, 100);
});

$('.textEdit').live('click', function (e) {
    blockRender = true;
});

然后在renderNote():

function renderNote( note ){
    var itemContent = $(note).val();

    if (blockRender) {
        blockRender = false;
        return false;
    }

    itemContent = htmlStrip(itemContent);
    itemContent = itemContent.replace(/\n/g, "<br/>");

    itemContent = itemContent
        .replace(/\[b\]/gi, "<b>")
        .replace(/\[\/b\]/gi, "</b>")
        .replace(/\[i\]/gi, "<i>")
        .replace(/\[\/i\]/gi, "</i>")
        .replace(/\[s\]/gi, "<s>")
        .replace(/\[\/s\]/gi, "</s>");

    $(note).replaceWith("<p class='notes'>"+ itemContent +"</p>");
}

http://jsfiddle.net/TsuZn/3/

【讨论】:

  • “正确使用其中任何一个都会继续逃避我”......我想我一定是唯一的一个。当用作“实时”替代品时,它似乎永远不会起作用,例如$('body').on('event','selector',func);。还是没搞清楚我是傻还是有什么不对劲..
  • 这可能会有所帮助:stackoverflow.com/questions/8161691/…
  • @KryponiteDove,实际上该答案给出的示例确实很有用。 jquery.com 上的文档很糟糕,甚至没有替换“live”的基本示例。感谢您的链接。
  • @KryptoniteDove - 我一头雾水,决定重写整个事情(JS 方面)。我现在唯一注意到的是,它不能直接从注释编辑直接到标题编辑(我将这两个条目更改为双击)。让我知道你的想法:jsfiddle.net/userdude/kS3FG
  • 是的,我也看到了 Jared,老实说,你的代码看起来比我的好很多,更干净整洁,不依赖于折旧的事件;) 会看一下,看看我能学到什么!非常感谢!
猜你喜欢
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多