代码之家  ›  专栏  ›  技术社区  ›  Fizzix

jQuery循环和追加会导致浏览器崩溃

  •  0
  • Fizzix  · 技术社区  · 10 年前

    因此,基本上,我试图在单击特定图像后显示引导模式。然后,我尝试在段落标记中显示一些文本和图像,该标记将每600毫秒更改一次,如下所示:

    Playing -> Playing. -> Playing.. -> Playing...

    一旦 text 变成 'Playing...' ,它将作为 'Playing' ,一次又一次,直到莫代尔 display 类变为 none .

    我是在一个 setTimeOut 函数,因为我需要等待Bootstrap Modal首先加载,以测试其类。

    虽然,我的代码每次都会使浏览器崩溃,因此,我无法查看开发人员工具来找出问题。

    $(".tutorial").click(function () {
        $('#tutorialModal').modal('show');
        $(this).toggleClass('playingGif'); //display 'playing' background image
        $(this).toggleClass('tutorial'); //turn off regular background image
    
        setTimeout(function () {
            while ($('#tutorialModal').css('display') == 'block') {
                var text = 'Playing';
    
                $('p', this)
                .delay(600)
                .queue(function (n) {
                    $(this).html(text);
                    n();
                });
    
                if (text == 'Playing...') {
                    text = 'Playing';
                } else {
                    text += '.';
                }
            }
        }, 500);
    });
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   Damon Smith    10 年前

    尝试如下操作:

    var text = 'Playing';
    function startPlaying() {
    
      if($('#tutorialModal').css('display') == 'block') {
    
    
        $('p', this).html(text);
        n();
    
        if (text == 'Playing...') {
          text = 'Playing';
        } else {
          text += '.';
        }
        window.setTimeout(startPlaying, 500);
      }
    }
    startPlaying();
    

    这样,它每500毫秒运行一次,更新播放。。。文本,然后如果它仍然显示,它将在500毫秒内再次运行该功能。我没有试过这段代码,它只是给你一个大致的想法。