Web Analytics Made Easy -
StatCounter recursive functions - CodingForum


No announcement yet.

recursive functions

  • Filter
  • Time
  • Show
Clear All
new posts

  • recursive functions


    can someone give me some advice

    the idea of the script is to create tool tips which while over the link or image will move with the mouse position

    i know i can use body tag to call function everytime mouse is moved and check a bool var to see if div is visible and hense move it

    however this means function call every time mouse moves

    so am trying to impliment recursive check when div is visible
    and switch it off when div is hidden

    call to show hide div also sets bool "initialize" to true or false

    function overlink()
    Moveit(userlay, Ey, Ex)
    setInterval(overlink(), 3000000);
    Moveit(userlay, 0, 0)

    problem is i keep getting stack overflow.......that javascript has reached its max recursive level even when i set interval to long time.

    am i doing something wrong here?


  • #2
    1. I don't see how recursiveism applies to your problem, but then sometimes I can be dense.

    2. You are applying recursion incorrectly. You have not provided a base case that will stop the recursive calls.

    Here is the general form of a recursive function:
    function Recursion (case) {
       if (base case) {
          do something
          return the result
       } else {
          simplify the case
          call the function with the simplified case
          return the result
    Let's say you want to add the integers 1 - 10... We'll start by passing in "10" and breaking it down. The base case is when the number reaches "1". At that point calling stops and we return with the results.

    Here's a working example:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <script language="JavaScript" type="text/javascript">
    function AddIntegers(aNumber) {
       total = 0;
       if (aNumber <= 1) {
          alert ("Returning with aNumber = " + aNumber);
          return (aNumber)   
       alert ("calling AddIntegers (" + (aNumber - 1) + ")");
       total = aNumber + AddIntegers(--aNumber);
       alert ("returning with total = " + total);
       return (total);
    } // function AddIntegers()
    <script language="JavaScript" type="text/javascript">
    sum = 0;
    theNumber = 10;
    sum = AddIntegers (theNumber);
    alert ("sum = " + sum);
    Recursion is not intuitive. But once you get it, it becomes easy. Really.

    There are three keys to making recursion work:[list=1][*]Identify the basic algorithm you want to implement[*]Identify the base case[*]Make the "Recursive Leap of Faith"[/list=1]

    As one textbook author put it, you must simply believe that recursion works. Because recursion is not intuitive, you will struggle with it until you make that leap of faith. And from my experience, that is absolutely true.

    Once I made the leap of faith it became really fun to make looping, counting as the sample above, etc. all recursive. Recursion is cool
    Last edited by RadarBob; Jul 31, 2002, 12:49 PM.