1

I came across this code to highlight in JavaScript. When I desected and ran on my own machine, and not jsFiddle I got the following error.

Uncaught RangeError: Maximum call stack size exceeded

http://jsfiddle.net/jme11/bZb7V/

To get around this, I looked at the other questions on StackOverflow and added in a setTimeout() function, but I still get the error. How come?

function getSelection() 
{
    var seltxt = '';

     if (window.getSelection) 
     { 
         seltxt = setTimeout(window.getSelection(), 5000); 
     } 
     else if (document.getSelection) 
     { 
         seltxt = setTimeout(document.getSelection(), 5000); 
     } 
     else if (document.selection) 
     { 
         seltxt = setTimeout(document.selection.createRange().text, 5000); 
     }
    else return;

    return seltxt;
}
plotplot
  • 293
  • 3
  • 11

1 Answers1

0

You have to pass a reference to the function to the setTimeout function. You are calling the function directly, should be as follows: function getSelection() { var seltxt = '';

 if (window.getSelection) 
 { 
     seltxt = setTimeout(window.getSelection, 5000); 
 } 
 else if (document.getSelection) 
 { 
     seltxt = setTimeout(document.getSelection, 5000); 
 } 
 else if (document.selection) 
 { 
     seltxt = setTimeout(document.selection.createRange, 5000); 
 }
else return;

return seltxt;
}

Not sure about the last one since you are calling .text after the function call.

taxicala
  • 21,408
  • 7
  • 37
  • 66