Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 2 of 2
  1. #1
    Junior Member Zdroyd's Avatar
    Join Date
    Nov 2010
    Posts
    5
    Member #
    24805
    Hello, for my site I have a graphic that I want to randomly change between different graphics of the same size. Here is my site: zdroyd.net the image that is under the header is the background of a table. I have the following code:

    Code:
    <script language="javascript">
    <!--
    onload = function() {
    /* If you change the directory, make sure you update it here. */
    var imageDirectory = "http://www.yourwebsite.com/images/";
    /* If you want to add more images on your own, put the the file name in the middle
    of the list here.  Make sure there are quotes around it and a comma at the end. */
    var imageArray = new Array("image3.gif",
    "image6.gif",
    "image1.gif",
    "image4.gif",
    "image2.gif",
    "image5.gif");
    var imageArrayLength = imageArray.length; // Leave these next few lines alone
    var pseudoRandomNumber = ( Math.round( Math.random() * (imageArrayLength - 1) ) );
    document.getElementById("tableImage").style.backgroundImage="url('" +
    "imageDirectory + imageArray[pseudoRandomNumber] + "')";
    }
    //-->
    </script>
    <noscript>
    <style type="text/css">
    #tableImage {
    background-image:url('http://www.yourwebsite.com/images/image3.gif');
    }
    </style>
    </noscript>
    
    <table id="tableImage" height="431" width="465">
    <tr><td> </td></tr>
    </table>
    This changes the tables background at random when the page loads, but I want it to also randomly change after a set time of seconds.

    Could someone help me with editing the code to do this?

    Thanks.

  2.  

  3. #2
    Senior Member paintingtheweb's Avatar
    Join Date
    Jul 2007
    Location
    Las Vegas
    Posts
    128
    Member #
    15503
    wrap your code in setInterval().

    Code:
    setInterval(function({
    //put your code in here
    }), 3000); //the 3000 number is the time in milliseconds. In this case it would be 3 seconds.


Remove Ads

Posting Permissions

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

Search tags for this page

java script code for random background images in the table

Click on a term to search for related topics.
All times are GMT -6. The time now is 05:21 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com