2

I am trying to evaluate some data entered into a textarea after a paste has occurred. When I use onkeyup it detects if fine and does the evaluation okay when the user uses Cntl-V to paste. It does nothing when they right-click and select paste. I changed to onkeyup to onpaste which detects both the Cntl-V and Right-click paste but the data is not there. I know that the onpaste is triggered before the actual paste is performed so I tried to delay the evaluation with a setTimeout(), but even with a 5 second delay it never gets the data until after function function completes. No matter what I do I can't seem to get a count of the number of items that were entered. Here is my code:

function delayStart() {                                                 

  alert("delayStart() function");                                       

  var inData = " ";                                                     
  setTimeout(function()                                                 
                {inData = document.getElementById("loanNumberPaste").value},
                5000);                                                      
  var iData = inData.value;                                             
  alert("iData = " + iData);                                            
  setTimeout(fnUpdCt(iData),5000);                                      
}                                                                       


function fnUpdCt(vId) {                                  

  var strVId = vId.value;                                
  alert("fnUpdCt() function \n" +                        
          "vId = " + strVId);                              

  var i;                                                 
  var iVal = vId.value;                                  
  var vCt  = 0;                                          

  iVal     = iVal.replace(/\s/g,' ');                    
  iVal     = iVal.split('  ');                           

  for (i=0; i < iVal.length; i++) {                      
         if (iVal[i].length > 0) {vCt++;}                  
  }                                                      

  document.getElementById("loanNumberCount").value = vCt;
}                                                        



<textarea id="loanNumberPaste" rows=17 cols=37 tabindex="1"          
             onpaste="delayStart();";onkeyup="fnUpdCt(this);"></textarea>

Any advise you can provide will be greatly appreciated.

AMIC MING
  • 6,306
  • 6
  • 46
  • 62

1 Answers1

3

The timeout isn't working because instead of passing the function by reference you're calling it immediately and trying to return its value.

Just wrap fnUpdCt in an anonymous function and it should be fine.

setTimeout(function(){fnUpdCt(iData)},5000);  

Also, you don't need to wait 5 seconds - its not the fact that its waiting x amount of time, its the fact that its asynchronous.

Snuffleupagus
  • 6,365
  • 3
  • 26
  • 36