Quote:
Originally Posted by headrift
I'm not quite sure what you're saying. If you need something called when the page is exited, that's body.onunload -- but it's probably not that easy.
So... you need something to stop the timer when the AJAX'd content changes? Seems like that should reside in another caller, like an onsubmit or onclick or something.
Either way, your timer needs to be globally available.
|
body.onunload is only when you leave the page, this is leaving the page AJAX-style. So the page is still there, only that the content is changed via ajax.
The thing is that one of those ajax pages is a slideshow and that's why the timer gets created when you click on that link and the content gets loaded into the div.
But if you click on another ajax link the content inside the div changes and the pictures are gone. So there is no slideshow anymore. That's why the timer has to stop.
I managed to put an if-clause around the javascript that does the slideshow so I don't get an error. But the problem is this:
1) User goes to my webpage
2) User clicks on "photos", ajax changes a div and it loads with photos and a javascript timer starts that changes the photos
3) User clicks on another link, ajax changes the div and the photos are gone. The timer still runs but it doesn't do anything (except hog the cpu cycles for nothing) since the if returns false.
4) User revisits the photos link and another timer start. Now I have two timers so the slideshow gets updated two times within each cycle.
5) Do step 3 and 4 over again and now I have three timers.
6) Repeat step 5 ten times and I have so many timers that constantly changes the photos that it is just a mess.
I could of course make a global timer that only get created once but I would prefer to just stop it if there is no need for it. As long as the timer runs it sends out ajax requests to the server and consumes bandwith, so it's not ideal.
I was hoping for some way in Rails that I could put some code inside ApplicationHelpers that gets called everytime a controller gets called, I could check which controller and which action that's requested and if it isn't the once for the photo slideshow I would just stop the timer if it exists.