javascript - Why words count mismatching with read word count? -
i using below code speed reading simulation. working fine. have pasted 500 words box, after read words, words count differing input words count.
$('#sim').each(function(){ this.contenteditable = true; }); var go = $('#go'); var stop = $('#stop'); var wordcount = 0; var wordcountbox = $('#wordcountbox'); var timepassed = $('#timepassed'); var textread = $('#textread'); go.on("click", function(e){ e.preventdefault(); startsim(); }); function startsim(){ var speed = $('#speed').val(); timestart = $.now(); var sim = $('#sim').text(); var wordarray = sim.split(" "); var simwrap = $('#sim'); var arrcount = wordarray.length; var alreadyread = []; (var = 0; < arrcount; i++) { (function(index) { settimeout(function() { var pos = index; if(pos < 0){ pos = 0; } alreadyread.push(wordarray[pos]); wordarray[pos] = '<b>'+wordarray[pos]+'</b>'; var words = wordarray.join(" "); simwrap.html(words); wordcount++; if(pos == (arrcount - 1)){ triggerdone(); } }, * speed); })(i); } // function done function triggerdone(){ wordcountbox.text(wordcount+' words read'); var timeend = $.now(); var timeres = timeend - timestart; timeres = parseint(timeres); timeres = timeres / 1000; timepassed.text(timeres+" seconds have passed"); alreadyread = alreadyread.join(""); textread.text(alreadyread); var summary = $('#summary'); summary.show(); return; } stop.on("click", function(e){ e.preventdefault(); triggerdone(); }); } #sim{ width:800px; height:300px; border:solid 1px #2e2e2e; color:#2e2e2e; padding:5px; overflow:auto; border:9px outset #0ada0a; margin-top:1em; } <div id="sim"></div> why calculating words count?
and 1 more doubt, how auto scroll, when reading words.
could please solve this?
the first issue with:
var wordarray = sim.split(" ");
in case, it's creating new "word" array each time space encountered... string like:
var words = "foo bar bim baz";
will return , array length of 6. use regex...
var wordarray = sim.split(/\s+/);
.. , solve problem.
Comments
Post a Comment