javascript - Dynamically updating dictionary on text entry -


lets have text box entering text. under have div displays entered text, highlighting words based on whether have been correctly spelt or not, follows:

var validity =  {     "a": true,     "aa": false,     "aaa": false };  document.getelementbyid("edit").addeventlistener('keyup', function () {     text_array = this.value.split(" ");      var output = [];     (var = 0; i< text_array.length; i++) {           output.push('<span class="' + group(validity[text_array[i]]) + '">' + text_array[i] + '</span>');     }      document.getelementbyid("text").innerhtml = output.join(" "); }); 

http://jsfiddle.net/barra/ac9nt/12/

i have php script can passed array of words , replies via json conveying if valid or not.

i wish make request script in dynamic fashion text entered. however, ajax call php script asynchronous , before first reply server, new request script might made user enters text box. how should deal this?

you can use timeout send request after typing has stopped x miliseconds. try this:

var timer; document.getelementbyid("edit").addeventlistener('keyup', function () {     cleartimeout(timer);     var timer = settimeout(function() {         text_array = this.value.split(" ");          var output = [];         (var = 0; i< text_array.length; i++) {               output.push('<span class="' + group(validity[text_array[i]]) + '">' + text_array[i] + '</span>');         }          document.getelementbyid("text").innerhtml = output.join(" ");          // send ajax request values     }, 500); }); 

this prevent request being made until 500ms after typing has stopped. can adjust value make more/less responsive needed.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -