Author Topic: [JavaScript] Help With Code  (Read 4159 times)

0 Members and 1 Guest are viewing this topic.

Offline kalan_vod

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2715
  • Rating: +10/-0
    • View Profile
    • kalanrock.us
[JavaScript] Help With Code
« on: January 29, 2011, 03:52:31 pm »
I have the code below, but it seems to be a bit slow. I have a broadband connection and the images it chooses are not big by any means..I am hoping to keep the JS not have to use PHP..It is a routine that randomly chooses a picture, any help would be great!

Code: [Select]
<script type="text/javascript">
if (document.getElementById) { window.onload = swap };
function swap() {
rndimg = new Array("newone", "blah", "bland", "blurp", "bookmark2", "boredom", "bunt", "directive", "floyd", "fusionbrick", "groundassult", "heartach2", "insight", "ism3", "purplenurple", "tinkr", "tunnel2");
x=(Math.floor(Math.random()*16));
randomimage=(rndimg[x]);
document.getElementById("background").style.backgroundImage = "url(images/"+ randomimage +".jpg)";
}
</script>

Offline Madskillz

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 488
  • Rating: +32/-2
    • View Profile
Re: [JavaScript] Help With Code
« Reply #1 on: January 29, 2011, 05:01:11 pm »
I made a routine like that once a while ago let me see if I can find it. My knowledge of javascript is fairly low, but I managed to get mine to work. I don't know enough so I couldn't tell you which things to try and avoid etc.

edit: mine did virtually the same thing, except for I used quotes.

You shouldn't need to do this:
Code: [Select]
randomimage=(rndimg[x]);
Instead you can just do this:
Code: [Select]
document.getElementById("background").style.backgroundImage = "url(images/"+ rndimg[x] +".jpg)";
At least I think that should be fine...it is in the rest of the programming world.

Here's my code if you wanna look at it:
Code: [Select]
var quotes = new Array('"People are using the web to build things they have not built or written or drawn or communicated anywhere else." - Tim Berners-Lee','"One persons data is another persons program." - Guy L. Steele, Jr. ','"The computer was born to solve problems that did not exist before." - Bill Gates','"If you think your users are idiots, only idiots will use it." - Linus Torvalds','"Low-level programming is good for the programmers soul." - John Carmack');
var container = Math.floor((Math.random() * (quotes.length)));
document.writeln(quotes[container]);
« Last Edit: January 29, 2011, 05:14:11 pm by Madskillz »

Offline kalan_vod

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2715
  • Rating: +10/-0
    • View Profile
    • kalanrock.us
Re: [JavaScript] Help With Code
« Reply #2 on: January 29, 2011, 06:50:52 pm »
Hey, thank you for your reply I will try that out. It worked, just seemed slow. Good to see you around here still buddy! Thanks for your code also

Offline Ikkerens

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 378
  • Rating: +28/-9
  • JavaScript Magician
    • View Profile
    • Walotech
Re: [JavaScript] Help With Code
« Reply #3 on: March 15, 2011, 02:00:11 pm »
What you could do is build in a preloader, not displaying any content (or something like "Loading...") until the images are loaded.
I'l post some code if you're interested ;)

Splut for Android [----------]
Paused/halted indefinitely, might be abandoned, our graphic designer quit and the rest of us simply doesn't have the time to work on it...