Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript can anyone expalin me the whats going on over here?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #162547
    sameershaik
    Participant
      var redLeft = parseInt($('#red').offset().left);
      function moveRed() {
        setTimeout(moveRed, 200);
        $('#red').css('left', ++redLeft);
      }
      moveRed();
    
    #162548
    Senff
    Participant

    The way I see it: first define how far the element with id “red” is placed from the left side of the screen. Then add that value to its left position every 200 ms.

    Or something.

    #162556
    sameershaik
    Participant

    first of all i wanna thank you Mr.Senff :)

    but “setTimeout()” method repet only one time right…..but when i run this code its contionuesly repeting like “setInterval()” method.

    may i know how this code is structured to work “setTimeout()” as “setInterval()” ?

    #162575
    Chromawoods
    Participant

    Well you have instructed setTimeout to execute moveRed, right? And what does moveRed do? It sets a timeout, which is instructed to run moveRed, which sets a timeout, which is instructed to run moveRed, which… and so on until the end of days.

    You have created a recursive function. If you want it do happen only once with a delay, then you can’t have that timer inside the moveRed function. So.. replace moveRed(); with setTimeout(moveRed, 200); and remove the timer from the moveRed function.

    ..hope I understood you right. :)

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.