-6

Can anyone please tell me how i can add an infinite loop or a constant repeat on this piece of JavaScript I have?

<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>
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
Dave Smith
  • 61
  • 1
  • 11
  • 1
    Which part do you want to have repeat constantly? – ruakh Nov 06 '12 at 18:53
  • Just make a for loop with `true` as the condition, although I'm not sure why you need this. – Asad Saeeduddin Nov 06 '12 at 18:53
  • 1
    your question is very vague. what exactly are you trying to do? – jbabey Nov 06 '12 at 18:54
  • What have you tried? What documentation did you read? Any research _at all_? – Lightness Races in Orbit Nov 06 '12 at 18:54
  • @Asad: Actually, no, that's wrong. – Lightness Races in Orbit Nov 06 '12 at 18:55
  • @LightnessRacesinOrbit Why is that wrong? – Asad Saeeduddin Nov 06 '12 at 18:56
  • @Asad: Try it and find out. These are jQuery effects, and they take time to complete. You can't just shove a loop around them and expect the effects to queue properly; the results will be either quite bizarre or not visible. Plus, you'll have locked the script into a synchronous block and nothing else will be able to run... including, possibly, the entire rest of the browser GUI. – Lightness Races in Orbit Nov 06 '12 at 18:59
  • @LightnessRacesinOrbit That information is not present in the question, and you are extrapolating here. I was assuming he wanted to keep adding click handlers in an infinite loop, which is why I was puzzled. – Asad Saeeduddin Nov 06 '12 at 19:02
  • 1
    @Asad: Huh? That _code_ is present in the question! Right there! Read it! Why on earth would anyone want to add click handlers in an infinite loop, when presented with the alternative that is the OP wants the effects to run infinitey? – Lightness Races in Orbit Nov 06 '12 at 19:07
  • Just asking a question on how to create an infinite loop seems strange, as most developers seem to figure out how to do that all on their own. – adeneo Nov 06 '12 at 19:16
  • Possible duplicate: http://stackoverflow.com/questions/9319573/how-to-repeat-loop-jquery-fadein-fadeout-fadein – Ivy Nov 06 '12 at 19:21

4 Answers4

1

I think what you should use here is setInterval. Otherwise your loop will block any other javascript you'd want to run.

JavaScript Timing Events

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
Ivy
  • 887
  • 1
  • 7
  • 25
  • I think you should use jQuery's event and actions stack as designed. – Lightness Races in Orbit Nov 06 '12 at 18:56
  • Well that's all fine, but I don't know how much good it does to point out that the info is inaccurate if you don't say what's wrong with it. I'd really appreciate an explanation. If you are saying to use the fadeTo method's callback parameter, it might be helpful to say so. – Ivy Nov 06 '12 at 19:04
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>
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
Smita
  • 4,634
  • 2
  • 25
  • 32
0

If you want to keep replaying some JavaScript, rather than looping use an interval or timer.

var interval = window.setInterval(function() {
    console.log('hi');
}, 1000);
Fenton
  • 241,084
  • 71
  • 387
  • 401
0

Infinite loop? just use following code.

while(true) {
   //do something here
}
Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
devsathish
  • 2,339
  • 2
  • 20
  • 16