2014-09-01 31 views
0

延遲後,我用它來觸發一次打字暫停在一個textarea啓動功能:jQuery 2.1 |觸發功能在textarea的

var keyTimer; 

$("#TEXTAREA").on('keyup mouseup', function(){ 

if (keyTimer) { 
clearTimeout(keyTimer); 
} 
keyTimer = setTimeout(function() { 
doFunction(); // Not working, triggers only once (on focus). 
}, 500); 

}); 

doFunction()只在第一KEYUP暫停(焦點)觸發。該功能必須在每次暫停(500)時觸發。現在我需要模糊TEXTAREA,然後重新集中它來重新激活對我無用的doFunction()。任何解決方案非常感謝

UPDATE

更新代碼:

var keyTimer; 

$("#TEXAREA").on('keyup mouseup', function(){ 

if (keyTimer) { 
clearTimeout(keyTimer); 
} 
keyTimer = setTimeout(function() { 
doFunction(); // Formulates a string 
$("#TEXAREA").trigger('change'); 
}, 500); 

}).on('change', function(){ 

alert('test'); // This triggers at every keyup/mouseup delay (500) 

// This section of code must work after delay (500), but it doesn't, 
// it only works on Textarea blur, which is not what I want: 

var txtarea = $(this); 
var livecount = $("#myValue").val(); // Comes from doFunction() 
if (livecount.length > 2800) { 
txtarea.css('color','#C00'); // Change text color of textarea 
} else if (livecount.length <= 2800) { 
txtarea.css('color','#000'); // Change text color of textarea 
} 

}); 
+0

你需要之間的逗號你的事件監聽器中的事件。 'Keyup,mouseup'。執行之前,你的函數是否等待了半秒? – tylerlindell 2014-09-01 13:50:09

+0

@tlindell使用此綁定方法不需要逗號之間的逗號。該代碼在500ms時觸發一個doFunction()。概率似乎是每500毫秒不發起textarea更改。我在textarea上觸發了一個變化,但仍然沒有任何變化。一個警報觸發功能,但到目前爲止textarea的變化是不會啓動;-( – koolness 2014-09-01 15:20:05

+0

@tlindell - 請參閱更新。Thx輸入。 – koolness 2014-09-01 15:48:32

回答

0

更新 - FINAL

好,我知道它的工作最後:

var keyTimer; 

$("#TEXTAREA").on('keyup mouseup', function(){ 

$(this).change(); // Need this to update keyboard character input 

}).on('change', function(){ 

if (keyTimer) { 
clearTimeout(keyTimer); 
} 

keyTimer = setTimeout(function() { 

doFunction(); // Formulates a string 

}, 200); // Delay before doFunction() so it won't slow down keyboard input 

var livecount = $("#myValue"); // String value from doFunction() 
if (livecount.val().length > 2800) { 
$(this).css('color','red'); // Change textarea text color to red for overlimit 
} else if (livecount.val().length <= 2800) { 
$(this).css('color','black'); // Change textarea text color to black for within limit 
} 

}); 

這是一個漫長的一天;-)

+0

提示 - 不要延遲函數執行,不會中斷鍵盤輸入,只有在絕對需要的情況下儘量減少延遲。 – koolness 2014-09-01 20:59:46

+0

我很高興你明白了! – tylerlindell 2014-09-02 03:24:31

0

它看起來像只覺得在你的代碼,缺少的是您正在使用的doFunction選擇。您正在使用#myValue,但實際上正在查找#TEXTAREA內部的值。這是我jsfiddle

function doFunction(){ 
    console.log('not a test') 
} 

var keyTimer; 

$("#TEXAREA").on('keyup mouseup', function(){ 

    if (keyTimer) { 
     clearTimeout(keyTimer); 
    } 

    keyTimer = setTimeout(function() { 
     doFunction(); // Formulates a string 
     $("#TEXAREA").trigger('change'); 
    }, 1000); 

    }).on('change', function(){ 

    console.log('test'); // This triggers at every keyup/mouseup delay (500) 

    // This section of code must work after delay (500), but it doesn't, 
    // it only works on Textarea blur, which is not what I want: 

    var txtarea = $(this); 
    var livecount = $("#TEXAREA").val(); // Comes from doFunction() 
    if (livecount.length > 2800) { 
     txtarea.css('color','#C00'); // Change text color of textarea 
    } else if (livecount.length <= 2800) { 
     txtarea.css('color','#f00'); // Change text color of textarea 
    } 

}); 
+1

@ tlindell - 我希望這是簡單的,但$(「#myValue」)。val ()包含我需要比較的所有連接的隱藏值的字符串,而不僅僅是從$(「#TEXAREA」)。val()。Thx用於種類輸入 – koolness 2014-09-01 20:38:35