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 表示结束操作
                                        }
                                    })
                                }
                            }
                        })
posted @ 2020-08-24 19:06  多喝热水,早点睡觉  阅读(1178)  评论(0编辑  收藏  举报