www.webdeveloper.com
Results 1 to 7 of 7

Thread: proper syntax for adding a counter to a function so it only fires once?

  1. #1
    Join Date
    Aug 2006
    Posts
    4

    Post proper syntax for adding a counter to a function so it only fires once?

    Pretty basic stuff here, just making a css based hover ad, but once the user hides it I don't want it to fire again, for obvious reasons, and also because the users mouse would be hovering over the div that would fire it when they tried to close it, so it is impossible to hide it.

    current sudo-code (sorry I'm not a programer) looks like this:

    ===============

    var count = 0; //set counter to 0

    // if counter is equal to 0, do this function,
    // which includes adding one to the counter at the end of the function
    // theoretically the next time it tries to fire it should find that
    // count no longer equals 0, and not fire

    function show_pop(){
    if (count == 0);{
    document.getElementById("box1").style.top = "300";
    count ++;}
    }

    function hide_pop(){
    document.getElementById("box1").style.top = "-600";
    }

    ============

    and it fires the first function, show_pop, like so:

    <div id="leftColumn" onMouseOver="show_pop();">

    and fires the second function, hide_pop, like this:

    <p><a href="JavaScript:hide_pop();">close window</a></p>


    Thanks folks

    - Jonny

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755
    Looks like it should work fine. Another method, but not necessarily better, would be below:

    Code:
    function show_pop(popped) {
      if (!popped)
        document.getElementById("box1").style.top = "300";
      return true;
    }
    Then you could call it by doing:
    Code:
    var adShown = false;
    ...
    onclick="adShown = show_pop(adShown);"

  3. #3
    Join Date
    Aug 2006
    Posts
    4
    hmm, well getting closer

    So doing this:

    <script language="JavaScript">

    var adShown = false;

    function show_pop(popped){
    if (!popped)
    document.getElementById("box1").style.top = "300";
    return true;
    }

    function hide_pop(){
    document.getElementById("box1").style.top = "-600";
    }

    </script>

    with this trigger:

    <p><a href="JavaScript:adShown = show_pop(adShown);">close window</a></p>

    it seems to do something, but then it just spits out a blank page with "true" on the screen.

    I need it to just run the hide_pop if show_pop = popped

  4. #4
    Join Date
    Aug 2006
    Posts
    4
    still not having any luck here

    now trying this:

    var count = 0

    if (count < 1)
    {
    function show_pop(){
    document.getElementById("box1").style.top = "300";
    count++;
    }
    }

    else (count > 0)
    {
    function hide_pop(){
    document.getElementById("box1").style.top = "-600";
    }
    }

    this can't be that complex can it???

  5. #5
    Join Date
    Mar 2006
    Posts
    60
    popVar=true;

    function show_pop(){
    if (popVar){
    document.getElementById("box1").style.top = "300";
    }
    }

    function hide_pop(){
    popVar=false;
    document.getElementById("box1").style.top = "-600";
    }

  6. #6
    Join Date
    Aug 2006
    Posts
    4

    resolved

    that did it

    thank you so much,

    - J

  7. #7
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles