while循环使浏览器忙

如何在浏览器不忙的情况下运行while循环?

我有需要无限运行的脚本:

do {
        $('#announcement').css('text-shadow','1px 1px 1px black, 0 0 25px blue, 0 0 5px darkblue');
        setTimeout(function()
        { $('#announcement').css('text-shadow', '1px 1px 3px white, 0 0 25px blue, 0 0 5px darkblue'); },1000);
      } while (true);

但是浏览器陷入困境……人们如何使用在浏览器中运行的while循环?

评论
  • JonDai
    JonDai 回复

    You can use setInterval instead of loop. Please note, add time in milliseconds in answer as per your need

    setInterval(() => {
            $('#announcement').css('text-shadow','1px 1px 1px black, 0 0 25px blue, 0 0 5px darkblue');
            setTimeout(function()
            { $('#announcement').css('text-shadow', '1px 1px 3px white, 0 0 25px blue, 0 0 5px darkblue'); },1000);
          },{time in millisecond})
    
  • uaut
    uaut 回复

    你不能

    If you want to toggle between two values every period of time, use setInterval and not a loop.