每隔X秒更改DIV的班级,但不仅要更改1级,而且要更改多个班级 [英] Change DIV's class every X second but not just 1 but with multiple classes

查看:91
本文介绍了每隔X秒更改DIV的班级,但不仅要更改1级,而且要更改多个班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过这段代码,并且知道它是如何工作的

I've seen this code already and know how it works

链接:使用jquery来切换类每5秒1秒

但是您将如何使用Multiple class?

But how would you do this with Multiple class?.

例如

  • 前5秒将添加"CLASS1"

  • first 5 second will add "CLASS1 "

接下来的5秒钟将添加"CLASS2"

next 5 seconds will add " CLASS2 "

,接下来的5秒钟将添加"CLASS3"

and next 5 seconds would add " CLASS3 "

,然后在完成所有操作后重复此操作.

and then repeat this after everything is done.

我只知道很少的jQuery并且仍在学习.任何帮助,将不胜感激.

I only know little jQuery and still learning. Any help would be appreciated.

推荐答案

要每隔x秒更改一次类,可以使用setInterval:

To change the class every x amount of seconds, you would use setInterval:

var counter = 1;
var int = setInterval(function(){
    $("div").attr("class", "class" + counter);
    if (counter === 3){
        counter = 1; // If counter = 3, set it back to 1 for next loop
    } else {
        counter++; // Else, add 1 to the counter
    }
}, 5000);

示例

如果要在x次迭代后停止该函数的运行,可以设置第二个计数器,然后使用clearInterval(int)以编程方式结束它.

If you'd like to stop the function from running after x amount of iterations, you could set up a second counter, and then use clearInterval(int) to programmatically end it.

这篇关于每隔X秒更改DIV的班级,但不仅要更改1级,而且要更改多个班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆