简体   繁体   English

反复调用函数进行淡入和淡出JQuery

[英]Call Function repeatedly for fade-in and fade-out JQuery

I have an array of words which I would like to fade-out and fade-in continuously. 我有一系列要连续淡出和淡入的单词。 I am very new to JS and I am not able to figure out. 我对JS非常陌生,无法弄清楚。

My code is as below: 我的代码如下:

animate_loop = function(){
        var showText = ["Security","Mobile/Wireless","Cloud/Database","PC/Storage"]
        $.each(showText, function(i, val) {
        setTimeout(function() {
            $('#animate').fadeOut("slow", function() {
                $(this).text(val).fadeIn("slow");
            });
        }, i * 3000);
        });

setInterval(function(){animate_loop();},5000)

With this code, the function loops through the array showText really fast and I was wondering if there is any other approach without a setInterval to achieve this. 有了这段代码,该函数真正快速地遍历了数组showText ,我想知道是否存在没有setInterval其他方法来实现这一点。 May be by just calling animate_loop function infinitely which I read is not advisable. 可能只是无限地调用animate_loop函数,我建议不animate_loop So any suggestions are welcome. 因此,欢迎提出任何建议。

other approach without a setInterval 没有setInterval的其他方法

Yes, what I've done here is use the callback's to keep a constant chain running. 是的,我在这里所做的是使用回调来保持恒定链的运行。 Basically fadeIn / fadeOut, re-run on the fadeOut. 基本上淡入/淡出,在淡出上重新运行。

 $(function () { var showText = ["Security","Mobile/Wireless", "Cloud/Database","PC/Storage"]; var showNum = 0, $showText = $('.showtext'); function doShow() { $showText.text(showText[showNum]); $showText.fadeIn('slow', function () { $showText.fadeOut('slow', function () { //lets make it so it wraps back to the start showNum = (showNum + 1) % showText.length; doShow(); }); }); } doShow(); }); 
 .showtext { font-size: 24pt; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="showtext"> </div> 

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

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