Create an endless loop in Jquery

后端 未结 6 1613

The HTML structure is like this

  • some Text
6条回答
  •  爱一瞬间的悲伤
    2021-01-03 10:13

    Edit:

    This code was written way too late at night by a tired programmer (myself), and should not be used due to browser hosing. Please see jQuery draws to a halt on Chrome and mac OS for production-quality code!

    Do not use the below code.


    Use two mutually-dependent functions:

    var $lis = $('ul.innerfade > li');
    
    function fadeThemOut()
    {
        $lis.fadeOut('slow', fadeThemIn);
    }
    
    function fadeThemIn()
    {
        $lis.fadeIn('slow', fadeThemOut);
    }
    
    // kick it off
    fadeThemOut();
    

    Demo: http://jsfiddle.net/mattball/nWWSa/


    You can write this more concisely using .fadeToggle():

    var $lis = $('ul.innerfade > li');
    
    function toggleThem()
    {
        $lis.fadeToggle('slow', toggleThem);
    }
    
    // kick it off
    toggleThem();
    

    Demo: http://jsfiddle.net/mattball/XdAEG/

提交回复
热议问题