简体   繁体   English

setInterval函数中的onClick切换类

[英]onClick toggle class inside setInterval function

setInterval(function () {
$("#bound").click(function () {
    $(".top").toggleClass("one");
    $(".middle").toggleClass("two");
    $(".bottom").toggleClass("three");
    $(".nav-menu").toggleClass("menu-show");
});}, 450);

What is wrong with it? 怎么了 Transition lags too much and sometimes stops workin. 过渡滞后太多,有时会停止工作。

What is wrong with it? 怎么了

The thing is you are binding so many click events at the interval of 450ms which will lead the browser to consume so much memory and browser will be unresponsive eventually. 问题是您以450毫秒的间隔绑定了许多click事件,这将导致浏览器消耗大量内存,并且浏览器最终将无响应。

Other noticeable thing is that you will get the "n" the number of bound events. 其他值得注意的是,您将获得绑定事件的数量“ n”。 So, as it is a continued process so at each iteration it will bind an event means if two iterations then two callbacks execution. 因此,由于它是一个连续的过程,因此在每次迭代时它将绑定一个事件,意味着如果执行两次迭代,则执行两次回调。

I guess you might instead do this: 我想您可能会这样做:

$("#bound").click(function () {
    setTimeout(function () { // <------change it to setTimeout if u don't make it infinite
        $(".top").toggleClass("one");
        $(".middle").toggleClass("two");
        $(".bottom").toggleClass("three");
        $(".nav-menu").toggleClass("menu-show");
    }, 450);
});

Incase you want the latency after click event, you should put setInterval function inside click function: 如果您想要单击事件后的延迟,则应将setInterval函数放在click函数中:

$("#bound").click(function () {
  setInterval(function () {
    $(".top").toggleClass("one");
    $(".middle").toggleClass("two");
    $(".bottom").toggleClass("three");
    $(".nav-menu").toggleClass("menu-show");
  }, 450);
});

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

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