javascript - variable defined in settimeout function returns as undefined outside the function -


please find code below:

settimeout(function() { var objtextareatext = objtextarea.innertext; var normalizedtext = objtextareatext;                         if (!countspacesaschars) {                             normalizedtext = objtextareatext.replace(/\s/g,"").replace(/&nbsp;/g, "");}                         strtext = normalizedtext.replace(/(\r\n|\n|\r)/gm,    "").replace(/&nbsp;/gi, " ");                                  //strip html tags                         strtext = normalizedtext.replace(/(<([^>]+)>)/ig,"").replace(/^([\t\r\n]*)$/, "");                     }, 50); if (strtext.length >= maxlength) {  } 

from above if statement strtext returning undefined.

also if include if statement inside settimeout function below:

settimeout(function() {     var objtextareatext = objtextarea.innertext;                 var normalizedtext = objtextareatext;                 if (!countspacesaschars) {                     normalizedtext = objtextareatext.replace(/\s/g,    "").replace(/&nbsp;/g, "");                 }                 strtext = normalizedtext.replace(/(\r\n|\n|\r)/gm, "").replace(/&nbsp;/gi, " ");                          //strip html tags                 strtext =         normalizedtext.replace(/(<([^>]+)>)/ig,"").replace(/^([\t\r\n]*)$/, "");                  if ((e.data.domevent.$.keycode === 8) ||     (e.data.domevent.$.keycode === 46) || ((e.data.domevent.$.shiftkey)                      && (e.data.domevent.$.keycode === 36)) || ((e.data.domevent.$.shiftkey) && (e.data.domevent.$.keycode === 35))                      || (e.data.domevent.$.keycode === 35) || (e.data.domevent.$.keycode === 36) || (e.data.domevent.$.keycode === 37)                      || (e.data.domevent.$.keycode === 38) || (e.data.domevent.$.keycode === 39) || (e.data.domevent.$.keycode === 40)) {                     showcharactercount();                     e.cancelbubble = false;                     e.returnvalue = true;                     return true;                 }                 // reaches max length - shows error max_value reached error msg.                 if (strtext.length >= maxlength) {                     showcharactercount();                     e.cancelbubble = true;                     e.returnvalue = false;                     e.cancel();                     e.stop();                     return false;                 } else {                     showcharactercount(strtext);                     e.cancelbubble = false;                     e.returnvalue = true;                     return true;                 }           }, 50); 

by above way there issue key events.

e.cancelbubble = true; e.returnvalue = false;                     e.cancel();                     e.stop();                     return false; 

these code not work.

so please suggest solution. please note: settimeout function should used getting count of characters rtf in salesforce.

use below code there closure added settimeout

settimeout(function() {  var objtextareatext = objtextarea.innertext; var normalizedtext = objtextareatext;                         if (!countspacesaschars) {                             normalizedtext = objtextareatext.replace(/\s/g,"").replace(/&nbsp;/g, "");}                         strtext = normalizedtext.replace(/(\r\n|\n|\r)/gm,    "").replace(/&nbsp;/gi, " ");                                  //strip html tags                         strtext = normalizedtext.replace(/(<([^>]+)>)/ig,"").replace(/^([\t\r\n]*)$/, "");    //change below code line closure   }(), 50);   // strtext not undefined  if (strtext.length >= maxlength) {   } 

if want use settimeout variable value outside , can using closure function , because it(a closure function) has access outside , inside , within function. more info @ closure function


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -