-6

JavaScript私が持っているこの部分に無限ループまたは一定の繰り返しを追加する方法を教えてください。

<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

4 に答える 4

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>
于 2012-11-06T18:57:47.427 に答える
1

ここで使うべきものはsetIntervalだと思います。そうしないと、ループによって、実行したい他のJavaScriptがブロックされます。

JavaScriptタイミングイベント

于 2012-11-06T18:54:52.300 に答える
0

ループするのではなく、JavaScriptを再生し続ける場合は、インターバルまたはタイマーを使用します。

var interval = window.setInterval(function() {
    console.log('hi');
}, 1000);
于 2012-11-06T18:55:44.703 に答える
0

無限ループ?次のコードを使用してください。

while(true) {
   //do something here
}
于 2012-11-06T19:09:51.350 に答える