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?
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):