循环javascript函数?

时间:2012-11-06 18:52:06

标签: javascript jquery

任何人都可以告诉我如何才能在JavaScript的这段{I}上添加无限循环或不断重复?

<script>
$(".modcontentnewestmore").hide();
$('.morebutton').click(function () {
    if ($('.modcontentnewestmore').is(":hidden")) {
         $(".modcontentnewest").fadeTo(500, 0);
         $('.modcontentnewestmore').fadeTo(0, 500);

    } else {

        $('.modcontentnewestmore').fadeTo(500, 0);
              $('.modcontentnewest').fadeTo(0, 500);

    }
  });

</script>

4 个答案:

答案 0 :(得分:1)

我认为你应该使用的是setInterval。否则你的循环将阻止你想要运行的任何其他JavaScript。

JavaScript Timing Events

答案 1 :(得分:1)

<script>
  function doSomething{
  $(".modcontentnewestmore").hide();
  $('.morebutton').click(function () {
      if ($('.modcontentnewestmore').is(":hidden")) {
           $(".modcontentnewest").fadeTo(500, 0);
           $('.modcontentnewestmore').fadeTo(0, 500);

      } else {

          $('.modcontentnewestmore').fadeTo(500, 0);
                $('.modcontentnewest').fadeTo(0, 500);

      }
    });
  }
  setInterval(doSomething, 30); //it will loop the function doSomething every 30 ms
</script>

答案 2 :(得分:0)

如果你想继续重播一些JavaScript,而不是循环使用间隔或计时器。

var interval = window.setInterval(function() {
    console.log('hi');
}, 1000);

答案 3 :(得分:0)

无限循环?只需使用以下代码。

while(true) {
   //do something here
}