简体   繁体   English

如何在同一事件Javascript中侦听click事件

[英]How to listen for click event inside same event Javascript

What I want is for the div element to move around until it is clicked again, upon which it should stop at its current location. 我想要的是让div元素四处移动,直到再次单击它,然后将其停在其当前位置。 Is there a specific way to listen for the same event inside the event? 在事件内部是否有监听特定事件的特定方法? Moving the div around is not the issue. 移动div并不是问题。 I just can't figure out how to get it to stop. 我只是不知道如何停止它。 Can anyone explain to me why what I have isn't working? 谁能向我解释为什么我的东西不起作用?

var clickTracker = false;

$("#div3").click(function() {
     if(clickTracker === false){
        var width = $(window).width();
        var height = $(window).height();
        for(var i = 0; i < 1000; i++){
            const randomLeft = Math.floor(Math.random() * (width - 200))-1;
            const randomHeight = Math.floor(Math.random() * (height - 200))-1;
            $("#div3").animate({left: '+='+randomLeft}, 500).animate({top: '+='+randomHeight}, 500);                            
            $("#div3").click(function() {
                clickTracker = true;
                break;
            });         
        }
    }
    else{
        clickTracker = false;
    }
});

 var clickTracker = false; $("#div3").click(function() { if (clickTracker === false) { clickTracker = true; var width = $(window).width(); var height = $(window).height(); for (var i = 0; i < 10; i++) { const randomLeft = Math.floor(Math.random() * ((width - currentPos.left) - 200)) - 1; const randomHeight = Math.floor(Math.random() * ((height - currentPos.top) - 200)) - 1; console.log("putting stuff in animation queue"); $("#div3").animate({ left: '+=' + randomLeft }, 5000).animate({ top: '+=' + randomHeight }, 5000); } } else { clickTracker = false; $("#div3").stop(true); } }); 
 #div3 { width: 100px; height: 100px; background: black; position: absolute; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="div3"></div> 

I understand why your first thought was to call the click function within the for-loop , but if you had looked in your console after clicking on it a second time, you would have seen that you were using break the wrong way -- by calling a new function you are changing the scope, and that break statement was trying to break out of the function (which you can't do, you would have to use a return for that) and not the loop. 我明白为什么你首先想到的是打电话内点击功能for-loop ,但如果你已经在你的控制台点击它第二次看了之后,你会看到你使用break了错误的方式-通过调用一个新函数,您正在更改范围,并且该break语句正尝试突破该函数(您不能执行此操作,因此必须使用return )而不是循环。 It's also not a good idea to have two handlers for the same event. 为同一事件设置两个处理程序也不是一个好主意。 That's the kind of thing that tends to confuse computers. 那是一种容易使计算机混乱的事情。 :) :)

It also might be worth pointing out that the for loop might not be working the way you think it is. 还可能需要指出, for loop可能无法按您认为的方式工作。 What it's actually doing (you can see this if you look at the console for the snippet) is populating an animation queue for the div. 它的实际操作(如果您查看代码段的控制台,您会看到这一点)正在为div填充动画队列。 That loop runs ten times (I changed it to ten for the sake of my sanity), generates a random number, and then puts that onto a list of places for the div to go, all in a matter of milliseconds. 该循环运行十次(出于理智起见,我将其更改为十),生成一个随机数,然后将其放入div可以通过的位置列表中,所有这些操作都在几毫秒内完成。 You can think of it like: $("#div3").animate().animate().animate() ... etc But the important thing to note here is that you are never clicking that div before the for loop ends. 您可以这样想: $("#div3").animate().animate().animate() ... etc但是这里要注意的重要一点是,您永远不要在for loop结束之前单击该div。 。

Read up on stop() here . 阅读上stop() 在这里

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

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