rotating images with javascript prob

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • howdy

    rotating images with javascript prob

    Hi all,
    I'm trying to rotate 5 images which load from the server. My script loads
    the images but when i move to the end of my array i want to cycle throught
    the array again so that the images will load again in succession. At the
    moment the images load through once and then stop on the first one. How can
    i make my code constantly cycle throuhg the images and not stop once it has
    gone through one time?
    Here is my current code:

    <SCRIPT language="JavaS cript">

    var pic_width=300;
    var pic_height=300;

    if (document.image s)
    {
    pic1= new Image(pic_width ,pic_height);
    pic1.src="http://www.ripnet.com/SilverHarbor/steveTEST/pics/pic1.jpg";
    pic2= new Image(pic_width ,pic_height);
    pic2.src="http://www.ripnet.com/SilverHarbor/steveTEST/pics/pic2.jpg";
    pic3= new Image(pic_width ,pic_height);
    pic3.src="http://www.ripnet.com/SilverHarbor/steveTEST/pics/pic3.jpg";
    pic4= new Image(pic_width ,pic_height);
    pic4.src="http://www.ripnet.com/SilverHarbor/steveTEST/pics/pic4.jpg";
    pic5= new Image(pic_width ,pic_height);
    pic5.src="http://www.ripnet.com/SilverHarbor/steveTEST/pics/pic5.jpg";
    }


    var pics= new Array(5)
    pics[0]=pic1.src;
    pics[1]=pic2.src;
    pics[2]=pic3.src;
    pics[3]=pic4.src;
    pics[4]=pic5.src;

    var numpics=5;
    var thenum=0;
    imgName="img1";

    function change_it()
    {
    if (document.image s)
    {
    document.write( "<IMG SRC='"+pics[thenum]+"' border='0'
    width='"+pic_wi dth+"' height='"+pic_h eight+"' name='img1'>\n" );
    setTimeout('cha nge_it2()',1000 );
    }
    }

    function change_it2()
    {
    var x=0;
    thenum+=1;

    if (thenum>numpics-1)
    {
    document[imgName].src=pics[0];
    }
    else
    {
    document[imgName].src=pics[thenum];
    x+=-1;
    setTimeout('cha nge_it2()',1000 );
    }
    }

    //-->
    </SCRIPT>

    <body>
    <SCRIPT language="JavaS cript">
    <!--
    change_it()
    //-->
    </SCRIPT>
    </body>

    kudos
    steve


  • Lasse Reichstein Nielsen

    #2
    Re: rotating images with javascript prob

    "howdy" <me@huh.com> writes:
    [color=blue]
    > I'm trying to rotate 5 images which load from the server. My script loads
    > the images but when i move to the end of my array i want to cycle throught
    > the array again so that the images will load again in succession. At the
    > moment the images load through once and then stop on the first one.[/color]

    [color=blue]
    > function change_it2()
    > {
    > var x=0;
    > thenum+=1;
    >
    > if (thenum>numpics-1)
    > {
    > document[imgName].src=pics[0];[/color]

    Insert here:
    thenum = 0;
    [color=blue]
    > }[/color]
    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • howdy

      #3
      Re: rotating images with javascript prob

      I tried that and it is the same as before. After it has gone through all the
      pics, it stops at the first pic andd oesn't rotate through again.
      regards
      steve

      "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
      news:vfqe5nwq.f sf@hotpop.com.. .[color=blue]
      > "howdy" <me@huh.com> writes:
      >[color=green]
      > > I'm trying to rotate 5 images which load from the server. My script[/color][/color]
      loads[color=blue][color=green]
      > > the images but when i move to the end of my array i want to cycle[/color][/color]
      throught[color=blue][color=green]
      > > the array again so that the images will load again in succession. At the
      > > moment the images load through once and then stop on the first one.[/color]
      >
      >[color=green]
      > > function change_it2()
      > > {
      > > var x=0;
      > > thenum+=1;
      > >
      > > if (thenum>numpics-1)
      > > {
      > > document[imgName].src=pics[0];[/color]
      >
      > Insert here:
      > thenum = 0;
      >[color=green]
      > > }[/color]
      > /L
      > --
      > Lasse Reichstein Nielsen - lrn@hotpop.com
      > DHTML Death Colors:[/color]
      <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
      > 'Faith without judgement merely degrades the spirit divine.'[/color]


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: rotating images with javascript prob

        "howdy" <me@huh.com> writes:
        [color=blue]
        > I tried that and it is the same as before. After it has gone through all the
        > pics, it stops at the first pic andd oesn't rotate through again.[/color]

        My bad. That's what happens when you post half of the solution.

        Try this function:
        ---
        function change_it2()
        {
        thenum++;
        if (thenum >= numpics)
        {
        thenum = 0;
        }
        document[imgName].src=pics[thenum];
        setTimeout(chan ge_it2,1000);
        }
        ---
        You never used "x" for anything in change_it2?

        If you know you are gounf to recall change_it2 every second, you might
        want to use setInterval once instead of setTimeout repeatedly.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • howdy

          #5
          Re: rotating images with javascript prob

          Yes that worked. Many thanks.
          Steve


          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:ptgm5kj6.f sf@hotpop.com.. .[color=blue]
          > "howdy" <me@huh.com> writes:
          >[color=green]
          > > I tried that and it is the same as before. After it has gone through all[/color][/color]
          the[color=blue][color=green]
          > > pics, it stops at the first pic andd oesn't rotate through again.[/color]
          >
          > My bad. That's what happens when you post half of the solution.
          >
          > Try this function:
          > ---
          > function change_it2()
          > {
          > thenum++;
          > if (thenum >= numpics)
          > {
          > thenum = 0;
          > }
          > document[imgName].src=pics[thenum];
          > setTimeout(chan ge_it2,1000);
          > }
          > ---
          > You never used "x" for anything in change_it2?
          >
          > If you know you are gounf to recall change_it2 every second, you might
          > want to use setInterval once instead of setTimeout repeatedly.
          >
          > /L
          > --
          > Lasse Reichstein Nielsen - lrn@hotpop.com
          > DHTML Death Colors:[/color]
          <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
          > 'Faith without judgement merely degrades the spirit divine.'[/color]


          Comment

          • Dr John Stockton

            #6
            Re: rotating images with javascript prob

            JRS: In article <ptgm5kj6.fsf@h otpop.com>, seen in
            news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
            posted at Sat, 25 Oct 2003 00:38:05 :-
            [color=blue]
            > function change_it2()
            > {
            > thenum++;
            > if (thenum >= numpics)
            > {
            > thenum = 0;
            > }
            > document[imgName].src=pics[thenum];
            > setTimeout(chan ge_it2,1000);
            > }[/color]

            OK; or

            thenum++ ; thenum %= numpics
            doc ...
            set ...

            IMHO, it is always best to avoid if statements, provided that the
            alternative is readily understandable after it has been written.

            doc ... [thenum++] ; thenum %= numpics
            set ...

            or

            doc ... [thenum = (thenum+1)%nump ics]
            set ...

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

            Comment

            Working...