I have a div being refreshed every x seconds or so. Within this div I have a textarea. What I’m trying to do is if the user starts to type in this textarea, then don’t refresh the div, otherwise, keep refreshing.
This is the JS to detect the keydown in the textarea, the problem I’m having is that it’s making typing in the textarea laggy because the JS keeps running for every keystroke. How I can I only make this run once?
<script type="text/javascript">
$("#comment_textarea").live('keydown', function() {
$('form #comment_textarea').elastic();
clearInterval(auto_refresh); auto_refresh = 0;
});
</script>
It’s most likely the
elasticplugin you’re calling, because clearing an interval every keystroke shouldn’t create much (if any) lag.If the
elasticplugin is something that changes thetextareawhen the user focus’ the input element then you can bind it to thefocusevent instead so it doesn’t run each keystroke:Notice I used
.delegate()instead of.live()..delegate()allows you to select a root element other thandocument.If you need to have the
elasticplugin call inside yourkeydownevent handler or are still having lag issues you can always check the value of theauto_refreshvariable:This code would go inside your
keydownevent handler.