Using IF inside a Function

Discussion in 'JavaScript' started by jb=], May 13, 2010.

  1. #1
    So I'm sorta new to javascript, but am not new to C++ which seems similar to javascript.

    Anyways, what I'm trying to do is have a function load when someone clicks on the page so I'm using.
    <body onClick="function();"> 
    Code (markup):
    To load the function when clicking on the page.

    The problem I have is I only want this action to happen once when the page is clicked on, and my first solution I came up with was an IF statement that uses a variable to decide if this function has been done before. Example:
    If (x==0) {
    function code;
    x=x+1;
    }
    Code (markup):
    Basic programing pretty much. But I can't figure out how to use it outside or inside a function both ways aren't working. I understand a function is just a group of scripts that are called, but I want to be able to control the controller of the function pretty much(if that makes sense to you).

    Much help would be appreciated. If this isn't a clear explanation tell me and I'll clear up whatever doesn't make sense. (it's late)
     
    jb=], May 13, 2010 IP
  2. chricholson

    chricholson Peon

    Messages:
    2
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Could you not put it the other way around? So something like this:

    functionName()
    {
    if (clickedOnce == 0)
    {
    //your code here


    //finish with
    clickedOnce = 1;
    }
    }
     
    chricholson, May 13, 2010 IP
    jb=] likes this.
  3. jb=]

    jb=] Peon

    Messages:
    476
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Worked perfectly, Thanks!
     
    jb=], May 13, 2010 IP