jquery 实现在事件完成后才能再次点击执行


        var doing=false;//定义一个全局变量 (在事件完成后才能再次点击执行)
                        $(".zan-btn").on("click",function(){
                            if(doing){
                                layer.msg("上一个操作未完成,请稍等!");
                            }else{
                                doing=true;//变量设为true,表示开始操作
                                var that = this
                                var upvoteStatus=$(that).attr("data-upvoteStatus")
                                if(upvoteStatus==0){
                                    sendReq("addUpvote","post","/shop/course/query",{id:videoId},function(res){
                                        if(res.status==1){
                                        // $(that).addClass("ele-hidden")
                                        // $(that).siblings(".no-zan").removeClass("ele-hidden")
                                        // var zanNum = Number.parseInt($(that).find('.zan-number').text())
                                        var zanNum=$(".upvoteNumber").text()
                                        $(".upvoteNumber").text(Number.parseInt(zanNum)+1)
                                        layer.msg("点赞成功",{icon:1})
                                        $(".icon-zan-video").addClass("icon-zan1 text-red").removeClass("icon-zan")
                                        $(".zan-btn").attr("data-upvoteStatus",1)
                                        doing=false; //变量改回false 表示结束操作
                                        }else if(res.status==-1){
                                            layer.msg(res.msg,{icon:2})
                                            doing=false; //变量改回false 表示结束操作
                                        }
                                    })
                                }else if(upvoteStatus==1){
                                    sendReq("updateUpvote","post","/shop/course/query",{id:videoId},function(res){
                                        if(res.status==1){
                                        var zanNum=$(".upvoteNumber").text()
                                        $(".upvoteNumber").text(Number.parseInt(zanNum)-1)
                                        layer.msg("已取消点赞",{icon:1})
                                        $(".icon-zan-video").addClass("icon-zan").removeClass("icon-zan1 text-red")
                                        $(".zan-btn").attr("data-upvoteStatus",0)
                                        doing=false; //变量改回false 表示结束操作
                                        }else if(res.status==-1){
                                            layer.msg(res.msg,{icon:2})
                                            doing=false; //变量改回false 表示结束操作
                                        }
                                    })
                                }
                            }
                        })


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2024 CODEPRJ.COM