简体   繁体   English

购物车加号按钮连续点击

[英]Shopping Cart Plus Button Consecutively Click

I have a shopping cart, every cart item has plus and minus button. 我有一个购物车,每个购物车项目都有加号和减号按钮。 I need when a user click 3 times (consecutively) plus button then call addtocart function after latest click. 我需要当用户单击3次(连续)加号按钮,然后在最后一次单击后调用addtocart函数。 Latest click mean between first click and third click there is delay time, so user can click 4 times. 最终点击是指第一次点击和第三次点击之间存在延迟时间,因此用户可以点击4次。 How can I do ? 我能怎么做 ?

why don't try with some counter, each time the user click on the button we sum 1+ to the counter, and when the counter reach 3, we foo something. 为什么不尝试使用某个计数器,每次用户单击按钮时,我们将1+加到计数器上,而当计数器达到3时,我们将进行foo操作。

here is the jsFiddle 这是jsFiddle

HTML HTML

<input type="button" id="button1" value="Shop" />

JS JS

var countClick = 0   
    $('#button1').click(function() {
        countClick++
            if(countClick === 3){
               console.log('counter reach ' + countClick)
               var myButton= document.getElementById('button1');
               myButton.style.display ='none';
               setTimeout (function(){
                  myButton.style.display ='inline'; //here we hide the button for 5seconds
               countClick = 0 // here we make the counter go back to 0 again
              },5000);
              //do stuff here like add to cart or something.
             }else{
             console.log('counter still on ' + countClick);
          }
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM