Forum Moderators: open

Message Too Old, No Replies

How to have my javascript keep going without restarting on page load?

         

Simone100

6:07 am on Oct 19, 2006 (gmt 0)

10+ Year Member



Hello, I have some javascript that rotates items. Problem is
as soon as you leave my web page and come back it starts over. Any way to keep it going all on its own so everything gets rotated?
Here's the code with the problem under the content, its set to change every hour, but starts over as soon as you revisit the page. Please let me know how to fix this. Thanks!

content[0] = "http://www.website.com/something.txt";
content[1] = "http://www.website.com/something.txt";
content[2] = "http://www.website.com/something.txt";

var counter = 0;
function secondsplus_iframe()
{ document.getElementById("dynstuff").src = content[counter];
counter++; if (counter == content.length)
counter = 0; } setInterval("secondsplus_iframe()", 3600000);

Fotiman

2:35 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Probably by setting a cookie with the last value.

Fotiman

2:36 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Probably by setting a cookie with the last value and reading in that cookie when the page loads to act as the starting point.

Simone100

12:28 am on Oct 20, 2006 (gmt 0)

10+ Year Member



Are you saying all javascript functions start on page onload?
Any idea how I would do the cookie situation? Thank you.

brucec

2:23 am on Oct 22, 2006 (gmt 0)

10+ Year Member



You could also do this with an Ajax request.

brucec

2:30 am on Oct 22, 2006 (gmt 0)

10+ Year Member



Yeah, Simone, onLoad event is always the default in Javascript, since the page well, loads. LOL

I see from your code that it is obvious that the counter starts from zero since your first line is var counter = 0;

You could use a cookie like the others above said, but I try not to use cookies anymore because people disable them in their web browsers. Using cookies will run you this risk and then what will you do?

Like I said, Ajax is probably the better way to go since it will work without worrying if someone turned off their cookies.

With Ajax, you can use a simple PHP or ASP script and have the script keep track of your variable. The good thing about using Ajax is that the web browser will never leave your web page.

Your first lines of Javascript should be:

var counter = AjaxCounter; NOT var counter = 0

The Ajax will keep track of your item number. It will then send that number to your PHP script to increment it by one and return it as an Ajax responseText property. From that point, your line of javascript will be document.getElementById("dynstuff").src = AjaxCounter instead of content[AjaxCounter]

From there, when your setInterval("secondsplus_iframe()", 3600000) activates, your src property will just change via the Http request created by the Ajax.

I hope this helps. Read up on Ajax at W3Schools. W3schools nails the basics of Ajax for you pretty quickly.

Bruce

Simone100

6:59 am on Oct 22, 2006 (gmt 0)

10+ Year Member



Cute bruce and thanks for the info. Before I switch to ajax I am trying something else, noted in my new thread maybe you have the answer. That way I don't have to use set enterval but am using "get" instead. Thanks!

webdesignbilling

9:14 am on Oct 22, 2006 (gmt 0)

10+ Year Member



.

brucec

9:16 am on Oct 22, 2006 (gmt 0)

10+ Year Member



What will you be using a GET statement with? How will your GET post back to the page?