1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Only allow a click every X seconds?

Discussion in 'JavaScript' started by bobby9101, May 7, 2007.

  1. #1
    Hi, I am using a onclick event to call a function.
    I need to do some junk inside that function, but only want to allow an if statement inside that function to be run a max of once every X seconds.
    I need to prevent the if statement from running twice in that period.
    How do I do this?
     
    bobby9101, May 7, 2007 IP
  2. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #2
    You can use the following example as a guide (replace 5000 with 1000 if you want 1 second margin instead of 5):
    
    <html>
    <head>
    
    <script type = "text/javascript">
    
    var last = 0;
    function f_click() 
    {
      var now = new Date();
      // alert( "now=" + now.getTime() + ", last=" + last );
      if ( (now.getTime() - last) < 5000 )
      {
        alert( "Please wait: " + (5000 - now.getTime() + last) + " milliseconds" );
      }  
      else
      {
        alert( "Ok" );
        last = now.getTime();
      }
    }    
      
    </script>
    
    </head>
    <body>
      <a href="#" onclick="javascript:f_click();">Click Here</span>
    </body>
    </html>
    
    Code (markup):
     
    ajsa52, May 8, 2007 IP
    bobby9101 likes this.
  3. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks very much
    rep added
     
    bobby9101, May 8, 2007 IP