]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Throttles updating of the character counter to reduce the performance impact on typin...
authorbrion <brion@smidge.(none)>
Sun, 9 Aug 2009 00:54:57 +0000 (17:54 -0700)
committerCraig Andrews <candrews@integralblue.com>
Sun, 9 Aug 2009 01:50:31 +0000 (21:50 -0400)
http://laconi.ca/trac/ticket/1462

js/util.js

index 0ffa92ca92bf4a8dc83627ae90619ff8a711840c..440701937ae63cc2645e38ad56db2b1e96d8cc53 100644 (file)
  */
 
 $(document).ready(function(){
+       var counterBlackout = false;
+       
        // count character on keyup
        function counter(event){
                var maxLength = 140;
                var currentLength = $("#notice_data-text").val().length;
                var remaining = maxLength - currentLength;
                var counter = $("#notice_text-count");
-               if (counter.text() != String(remaining)) {
-                       counter.text(remaining);
-               }
+               
+               if (remaining.toString() != counter.text()) {
+                   if (!counterBlackout || remaining == 0) {
+                        if (counter.text() != String(remaining)) {
+                            counter.text(remaining);
+                       }
 
-               if (remaining < 0) {
-                       $("#form_notice").addClass("warning");
-               } else {
-                       $("#form_notice").removeClass("warning");
-               }
+                        if (remaining < 0) {
+                            $("#form_notice").addClass("warning");
+                        } else {
+                            $("#form_notice").removeClass("warning");
+                        }
+                        // Skip updates for the next 500ms.
+                        // On slower hardware, updating on every keypress is unpleasant.
+                        if (!counterBlackout) {
+                            counterBlackout = true;
+                            window.setTimeout(clearCounterBlackout, 500);
+                        }
+                    }
+                }
+       }
+       
+       function clearCounterBlackout() {
+               // Allow keyup events to poke the counter again
+               counterBlackout = false;
+               // Check if the string changed since we last looked
+               counter(null);
        }
 
        function submitonreturn(event) {