I have a random phrase (js ideas) on my html page that works when I refresh the page. But I do not want to refresh the whole web page since other things are happening. It gets the phrases from an external js file. I need to refresh it every few seconds - 3 to 5 will work. How do I make this phrase dynamically refresh without refreshing the whole web page? <div align="center"> <p class="p"> <strong>What if <script language="JavaScript" type="text/javascript"> //<![CDATA[ document.write(ideas[rand(ideas.length)]); //]]> </script> <br /> That might work.</strong> </p> </div> HTML: Any assistance would be appreciated. Thanks,
I'll assume that your "ideas" variable is already there and a JavaScript array... You could do something like this: var ideas = ["Idea 1", "Idea 2", "Idea 3", "Idea 4"]; <div id="ideas"></div> function new_item() { document.getElementById("ideas").innerHTML = ideas[Math.floor(Math.random() * ideas.length)]; } new_item(); setInterval(new_item, 5000); HTML: That will insert a random item from the "ideas" array inside the div with id="ideas" property, and then change it every 5 seconds (5,000 milliseconds).
Thanks, My array is in a long list. It starts with: // Display Random Quotes // Quotes Array Function function makeArray(len) { for (var k = 0; k < len; k++) this[k] = null; this.length = len; } // Quotation array ideas = new makeArray(301); ideas[0] = "first idea line is here."; and goes to ideas[300] = "last idea line is here."; It ends with: // Seed Function function rand(n) { seed = (0x015a4e35 * seed) % 0xfffffff; return (seed >> 20) % n; } // Random function var rightnow = new Date(); var seed = rightnow.getTime() % 0x7fffffff; I don't think I can list them like your script line shows ("Idea 1", "Idea 2", "Idea 3", "Idea 4", etc). It would be too long since I have 301 of them. Or will your script get one line at a time from my list without all that? This is all very new to me. I was happy just figuring out how to do the random thing form a list. Is there a way to wrap my DIV in a timer?
You can generate the array however you want... Really no right or wrong way to do it. It basically just takes 1 random item from the array named "ideas" every 5 seconds. As far as how you *get* that array, that's up to you since that's going to just be all your random things to show. Not sure what you mean by a random timer? Like a countdown timer, or?
My bad. I deleted that 'random' word after you answered. I was just looking for another way. I am truly new to this. So, how do I make my long list array work with your code?
Just take out the line I had that defines the array. Since yours is named the same thing, it should be fine.