SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Oct 2010
    Location
    Portland, Or
    Posts
    728
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    timed javascript slideshow.

    currently using this code for to do an "on refresh" slideshow.
    Code:
    var image = new Array()
    
    image[0] = "imgArray/gallery01.jpg"
    image[1] = "imgArray/gallery02.jpg"
    image[2] = "imgArray/gallery03.jpg"
    image[3] = "imgArray/gallery04.jpg"
    image[4] = "imgArray/gallery05.jpg"
    image[5] = "imgArray/gallery06.jpg"
    image[6] = "imgArray/gallery07.jpg"
    image[7] = "imgArray/gallery08.jpg"
    image[8] = "imgArray/gallery09.jpg"
    image[9] = "imgArray/gallery10.jpg"
    
    function randomNumber(n){
    number = Math.floor(Math.random() * (n + 1));
    return number;
    and later in the page in a div
    Code:
    <script type="text/javascript"> document.write( "<img src = '" + image[randomNumber(image.length -1)]+ "'>" );</script>

    is there a super simple way to change this so it is timed instead?
    thx
    Sherpa

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    var image = new Array()
    
    image[0] = "http://www.vicsjavascripts.org.uk/StdImages/Egypt5.jpg"
    image[1] = "http://www.vicsjavascripts.org.uk/StdImages/Egypt6.jpg"
    image[2] = "http://www.vicsjavascripts.org.uk/StdImages/Egypt7.jpg"
    image[3] = "http://www.vicsjavascripts.org.uk/StdImages/Egypt8.jpg"
    image[4] = "http://www.vicsjavascripts.org.uk/StdImages/Egypt9.jpg"
    
    function randomNumber(id,ary,ms){
     var src = ary[Math.floor(Math.random()*ary.length)],img=document.getElementById(id),ms=typeof(ms)=='number'&&ms>100?ms:1000;
     if (img){
      img.src=src;
     }
     setTimeout(function(){ randomNumber(id,ary,ms) },ms);
     return src;
    
    }
    /*]]>*/
    </script></head>
    
    <body>
    <script type="text/javascript">
     document.write( "<img id='tst' src = '" + randomNumber('tst',image,1000)+ "'>" );
     </script>
    </body>
    
    </html>

  3. #3
    SitePoint Guru
    Join Date
    Oct 2010
    Location
    Portland, Or
    Posts
    728
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Thank you, Will go try it out!

    was wondering what is:
    <![CDATA[*/ for? have not seen this before.

    D

  4. #4
    It's all Geek to me silver trophybronze trophy
    ralph.m's Avatar
    Join Date
    Mar 2009
    Location
    Melbourne, AU
    Posts
    24,198
    Mentioned
    456 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by pdxSherpa View Post
    was wondering what is:
    <![CDATA[*/ for? have not seen this before.
    It's needed when you are embedding JavaScript and CSS code in an XHTML document. (These both contain characters that aren't allowed in an XHTML document, so the CDATA wrapper kind of seals them off from the rest of the document.) Another reason not to bother with XHTML.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,815
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ralph.m View Post
    It's needed when you are embedding JavaScript and CSS code in an XHTML document.
    No it isn't - if you were embedding JavaScript in an XHTML document (rather than writing your JavaScript properly as a separate file) then you wouldn't be commenting those tags out. With the tags commented out the page can be identified as HTML rather than XHTML and the person that wrote the code can be identified as someone who doesn't know the difference between HTML and XHTML.

    The only current reason not to bother with XHTML is that IE8 doesn't support it and is still used by too many people to have web pages that they have offered as downloads rather than displayed in their browser. Only once IE8 is dead will XHTML become possible - that's why there is a new proposed version called XHTML 5.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •