Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. javascript setTimeout and return logic

javascript setTimeout and return logic

Scheduled Pinned Locked Moved Web Development
csharpjavascriptvisual-studiodebuggingtools
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    compninja25
    wrote on last edited by
    #1

    Hi guys! I can't figure out what the heck I'm doing wrong. I'm making a (hoping to at least) simple function that will take a white border and fade it to black on a mouseOver. Here's what I have so far:

    <script type="text/javascript">
    function animation(object, direction, number) {
    if (!number) {
    if (direction == "out") number = 255;
    if (direction == "in") number = 0;
    }

          var color = "rgb(" + number + "," + number + "," + number + ")";
          object.style.borderColor = color;
          
                    
          if (direction == "out") {
              if (number == 0) return;
                  
              var newNumber = number - 1;
              window.setTimeout(animation(object, direction, newNumber), 500);
          }
          
          if (direction == "in") {
              if (number == 255) return ;
              
              var newNumber = number + 1;
              window.setTimeout(animation(object, direction, newNumber), 500);                       
          }
      }
    

    </script>

    Now, what happens (when I use the debugger in visual studio 2008), the function executes correctly until the number variable gets to 255. At 255, the system does see the if (number == 255) return; part and appears to execute it. What happens after that when I hit F11 to continue stepping into the code, it jumps down to the window.setTimeout line again and tell's me there's an invalid argument. What's really weird, is that the number variable has now gone back to 254! I've even tried changing the if statement to stop at 250 and other various numbers, but it still seems to subtract a number and then give me the failure insted of stopping execution. Looking at other setTimeout examples, I also tried different syntax including wrapping the function in quotes, but this is the only way I've been able to get it to actually call the function. Wrapping it in quotes seems to just jump to the next line and return. I'm still fairly new to javascript, so I wouldn't doubt that there's something wrong with the flow of my logic, but I'm to the point where I feel like I'm not getting anywhere. Hopefully another set of eyes might see what's not obvious to me. Thanks!

    Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

    C N 2 Replies Last reply
    0
    • C compninja25

      Hi guys! I can't figure out what the heck I'm doing wrong. I'm making a (hoping to at least) simple function that will take a white border and fade it to black on a mouseOver. Here's what I have so far:

      <script type="text/javascript">
      function animation(object, direction, number) {
      if (!number) {
      if (direction == "out") number = 255;
      if (direction == "in") number = 0;
      }

            var color = "rgb(" + number + "," + number + "," + number + ")";
            object.style.borderColor = color;
            
                      
            if (direction == "out") {
                if (number == 0) return;
                    
                var newNumber = number - 1;
                window.setTimeout(animation(object, direction, newNumber), 500);
            }
            
            if (direction == "in") {
                if (number == 255) return ;
                
                var newNumber = number + 1;
                window.setTimeout(animation(object, direction, newNumber), 500);                       
            }
        }
      

      </script>

      Now, what happens (when I use the debugger in visual studio 2008), the function executes correctly until the number variable gets to 255. At 255, the system does see the if (number == 255) return; part and appears to execute it. What happens after that when I hit F11 to continue stepping into the code, it jumps down to the window.setTimeout line again and tell's me there's an invalid argument. What's really weird, is that the number variable has now gone back to 254! I've even tried changing the if statement to stop at 250 and other various numbers, but it still seems to subtract a number and then give me the failure insted of stopping execution. Looking at other setTimeout examples, I also tried different syntax including wrapping the function in quotes, but this is the only way I've been able to get it to actually call the function. Wrapping it in quotes seems to just jump to the next line and return. I'm still fairly new to javascript, so I wouldn't doubt that there's something wrong with the flow of my logic, but I'm to the point where I feel like I'm not getting anywhere. Hopefully another set of eyes might see what's not obvious to me. Thanks!

      Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      window.setTimeout needs a function-"pointer" as first argument. If I'm right this can only be a function without arguments. window.setTimeout(animation, 500); <- rigth way window.setTimeout(animation(object, direction, newNumber), 500);   <- wrong Starts the function animation direct without waiting 500ms.

      1 Reply Last reply
      0
      • C compninja25

        Hi guys! I can't figure out what the heck I'm doing wrong. I'm making a (hoping to at least) simple function that will take a white border and fade it to black on a mouseOver. Here's what I have so far:

        <script type="text/javascript">
        function animation(object, direction, number) {
        if (!number) {
        if (direction == "out") number = 255;
        if (direction == "in") number = 0;
        }

              var color = "rgb(" + number + "," + number + "," + number + ")";
              object.style.borderColor = color;
              
                        
              if (direction == "out") {
                  if (number == 0) return;
                      
                  var newNumber = number - 1;
                  window.setTimeout(animation(object, direction, newNumber), 500);
              }
              
              if (direction == "in") {
                  if (number == 255) return ;
                  
                  var newNumber = number + 1;
                  window.setTimeout(animation(object, direction, newNumber), 500);                       
              }
          }
        

        </script>

        Now, what happens (when I use the debugger in visual studio 2008), the function executes correctly until the number variable gets to 255. At 255, the system does see the if (number == 255) return; part and appears to execute it. What happens after that when I hit F11 to continue stepping into the code, it jumps down to the window.setTimeout line again and tell's me there's an invalid argument. What's really weird, is that the number variable has now gone back to 254! I've even tried changing the if statement to stop at 250 and other various numbers, but it still seems to subtract a number and then give me the failure insted of stopping execution. Looking at other setTimeout examples, I also tried different syntax including wrapping the function in quotes, but this is the only way I've been able to get it to actually call the function. Wrapping it in quotes seems to just jump to the next line and return. I'm still fairly new to javascript, so I wouldn't doubt that there's something wrong with the flow of my logic, but I'm to the point where I feel like I'm not getting anywhere. Hopefully another set of eyes might see what's not obvious to me. Thanks!

        Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        I'd look at the JQuery[^] library to do this and save yourself a whole lot of hassles.


        only two letters away from being an asset

        C 1 Reply Last reply
        0
        • N Not Active

          I'd look at the JQuery[^] library to do this and save yourself a whole lot of hassles.


          only two letters away from being an asset

          C Offline
          C Offline
          compninja25
          wrote on last edited by
          #4

          oh that is cool! To be honest I've never really looked into jQuery but you're right. It'll probably make most sense to just use that animate function they have. Thanks!

          Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups