Preventing alert to show two popups when button is clicked twice
Lets say we have 开发者_开发百科a js function which shows a pop up on a button click. But if the button is clicked twice in a hurry it show two popups. Is there any way to prevent alert to show two pop ups when the button is clicked twice in a hurry?
you could set a variable to store a flag to say the alert is already been shown?
//outside the event
var flagShown = false;
// in the event
if(!flagShown){
alert();
flagShow = true;
}
I havent tested this code...
HTH
Edit: this will make it show only once. You would have to reset the flag based on a timer or on some other event.
using a var that store the fact that a display is under way ?
Could not check as I'm not able to clic fast enough for 2 clics to be triggered...
var alert_under_way=0;
function doIt()
{
if (! alert_under_way)
{
alert_under_way =1;
alert("toto");
alert_under_way =0;
}
}
I have used the following code to prevent the display of two popups:
var prevent_popup=false;
function show_popup()
{
.....
.....
if(!prevent_popup)
alert("some text");
prevent_popup=true;
setTimeout('prevent_popup=false;',1000);
.....
.....
}
You could also disable the button once its been clicked and enable it again if you need to.
精彩评论