如何在函数完成javascript之前禁用单击按钮

时间:2013-05-18 03:27:31

标签: javascript function button timer timeout

好的,所以我有一个没有任何Id的按钮但是当它被点击时它会启动一个名为buttonClick的功能。这个功能基本上控制一个老虎机(戳机)它的图像,奖金等。注意,一些代码可能无法工作,但请忽略这一点,并想象它的工作。但是我怎么能这样做,以便如果用户在此功能完成之前点击播放按钮它将不会做任何事情,但仍将完成它已经在做的功能?很抱歉在这里没有我的全长代码给我带来了不便,只是一个样本。

function buttonClick(){
var pic = new Array("1.jpg","2.jpg","3.jpg","4.jpg","5.jpg");
var ex1 = pic[Math.floor(Math.random()*pic.length];
var ex2 = pic[Math.floor(Math.random()*pic.length];
var ex3 = pic[Math.floor(Math.random()*pic.length];
var moveImg = new Array("1.gif","2.gif","3.gif");    
var timer = new Array("1000","5000","500","2000");
var time =  Math.floor(Math.random()*4);
    if (document.getElementById('coins').value > 0){
            document.getElementById('coins').value -= 1;
            pic[0].src = moveImg[0];
            pic[1].src = moveImg[1];
            pic[2].src = moveImg[2];
            setTimeout(function(){ 
                pic[0].src = ex1;
                pic[1].src = ex2;
                pic[2].src = ex3;
           },timer[time])   
   }
        else{
            alert('Error insufficient tokens');
        }
}

1 个答案:

答案 0 :(得分:2)

var in_progress = false;
function buttonClick(){
   if(!in_progress){
     var pic = new Array("1.jpg","2.jpg","3.jpg","4.jpg","5.jpg");
     var ex1 = pic[Math.floor(Math.random()*pic.length];
     var ex2 = pic[Math.floor(Math.random()*pic.length];
     var ex3 = pic[Math.floor(Math.random()*pic.length];
     var moveImg = new Array("1.gif","2.gif","3.gif");    
     var timer = new Array("1000","5000","500","2000");
     var time =  Math.floor(Math.random()*4);
     if (document.getElementById('coins').value > 0){
        in_progress = true;
        document.getElementById('coins').value -= 1;
        pic[0].src = moveImg[0];
        pic[1].src = moveImg[1];
        pic[2].src = moveImg[2];
        setTimeout(function(){ 
            in_progress = false;
            pic[0].src = ex1;
            pic[1].src = ex2;
            pic[2].src = ex3;
       },timer[time])   
     } else {
        alert('Error insufficient tokens');
    }
  }
}

或者你可以将函数包装在一个闭包中,使in_progress成为一个私有变量。

var buttonClick = (function(){
  var in_progress = false;
  return function(){
   if(!in_progress){
     var pic = new Array("1.jpg","2.jpg","3.jpg","4.jpg","5.jpg");
     var ex1 = pic[Math.floor(Math.random()*pic.length];
     var ex2 = pic[Math.floor(Math.random()*pic.length];
     var ex3 = pic[Math.floor(Math.random()*pic.length];
     var moveImg = new Array("1.gif","2.gif","3.gif");    
     var timer = new Array("1000","5000","500","2000");
     var time =  Math.floor(Math.random()*4);
     if (document.getElementById('coins').value > 0){
        in_progress = true;
        document.getElementById('coins').value -= 1;
        pic[0].src = moveImg[0];
        pic[1].src = moveImg[1];
        pic[2].src = moveImg[2];
        setTimeout(function(){ 
            in_progress = false;
            pic[0].src = ex1;
            pic[1].src = ex2;
            pic[2].src = ex3;
       },timer[time])   
     } else {
        alert('Error insufficient tokens');
    }
  }
};
})();