Reputation: 1
$(document).ready(function(){
var keyUpTime = 0;
var t = 0;
var executeAfterOneSecond = false;
$('#source').keyup(function(){
if(executeAfterOneSecond == false){
executeAfterOneSecond = true;
t = setTimeout(function(){
executeAfterOneSecond = false;
sendValue($('#source').val());
}, 600);
}
keyUpTime = $.now();
setTimeout(function(){
if($.now() - keyUpTime >= 200) {
clearTimeout(t);
executeAfterOneSecond = false;
sendValue($('#source').val());
}
},200);
});
});
<textarea id="source" name="text" wrap="soft" tabindex="0" dir="ltr" spellcheck="false" autocapitalize="off" autocomplete="off" autocorrect="off" style="box-sizing: border-box; overflow-y: hidden; overflow-x: auto; padding-right: 20px; " class="oggy-textarea">
</textarea>
Well, I want my code works on change(); [$('#source').change(function(){---});], not on keyup(), but it doesn't. I tried with bind() but nothing. What's wrong?
Upvotes: 0
Views: 308
Reputation: 30747
The Change event only happens when the input
control loses focus
- hence why it's not firing.
See here under notes: http://www.w3schools.com/jquery/event_change.asp
You have to go with the keyUp
, keyPress
or keyDown
you could really clean up your methods too. Something like the following might be more stable.
This will call sendValue()
after 1 second after the last keyup. Takes your 22 lines down to 6 :)
Here is the code below as an example if jsFiddle
$(document).ready(function(){
$("#source").keyup(function(){
clearTimeout(this.timer);
this.timer = setTimeout(function() {sendValue($("#source").val());}, 1000);
});
});
Upvotes: 1