LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2009, 01:53 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Ruby on Rails: run script on leaving page


I have a javascript timer I need to shut down as the user leaves the page. The problem is, though, that I use AJAX on the page, and that counts as "leaving the page" since the AJAX script changes the content that the timer acts on.

Maybe there's a way to put a global piece of code that runs everytime a controller gets called, which could check what controller the user is calling. And if it doesn't match one specific controller (ie, the user is viewing something other than the part which my timer acts on) I can just stop the timer.

Would that work? Any other ideas?
 
Old 03-16-2009, 09:45 PM   #2
headrift
Member
 
Registered: Sep 2005
Distribution: Gentoo, Sabayon, Puppy, Arch
Posts: 165

Rep: Reputation: 29
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.
 
Old 03-16-2009, 10:04 PM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Quote:
Originally Posted by headrift View Post
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.
 
Old 03-16-2009, 10:25 PM   #4
headrift
Member
 
Registered: Sep 2005
Distribution: Gentoo, Sabayon, Puppy, Arch
Posts: 165

Rep: Reputation: 29
I don't know Rails, sorry... hopefully someone else does. If you end up doing it by hand, try this:

Check which content you're displaying in your div, using regexp or somesuch. Use that to set a flag variable in the AJAX request, and then check the reply for the "continue" flag. If it's not there, kill the timer.

Might be easier than doing it all client side. *shrug*

Best of luck.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Any issues installing Ruby Gems and Ruby on Rails in Slackware? Lufbery Slackware 8 02-09-2011 07:22 PM
LXer: Introducing LinRails: Run Ruby on Rails in Linux Easy LXer Syndicated Linux News 0 07-03-2007 10:16 AM
PHP v Ruby/Ruby on Rails Neko_D Linux - General 4 02-08-2007 11:56 PM
Can you help me with search in Ruby on Rails? viniosity Programming 1 01-05-2007 06:25 PM
LXer: Ruby off the Rails LXer Syndicated Linux News 0 12-24-2005 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration