Jump to content


Photo

Unsure if I should use Javascript here...


  • Please log in to reply
3 replies to this topic

#1 dark dude

dark dude
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 28 July 2006 - 12:26 PM

I'm currently working on a Php/MySQL based game that requires a server-side background delay to immitate waiting for it to march out, attack and then return...

In script terms, I want a user to be able to submit a form at any time and it then delays the processing script until a specific amount of seconds/minutes/hours/days has passed.
During this delay period, the user should be able to navigate the site, logout, go on holiday, whatever, without breaking the script.

So, I thought about making it so when you submit the form, it sends the details to a server background script or summat, and then, even if you're not viewing the script, once the time passes, the script activates.

Sounds well and good... but... How do I do it??? Some said Cron job, but I highly doubt i'd be able to Cron job a script saying "If ($timecompletion > time()) then..." to run every second of the day...

So, would Javascript be any help? It seems the most probable to help...

#2 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 28 July 2006 - 12:53 PM

The PHP function sleep($secstodelay); will delay execution of your PHP code; however, if the client has, in the intervening time, browsed to another URL, I do not think the completed page will load.  If I am wrong about that, then your problem is solved.  If I am correct, I have another option.

Use frames, one hidden and one visible.  When the user clicks for your delayed window, set a JavaScript delay in the hidden frame, which, when time is up, changes the window.location.href property of the visible frame.  That way, users can go whereever they want in the visible frame and your delayed page will load when ready. 

Keep in mind that once the href is changed, there can be another delay for the page to actually load in the window.  During that time, if the user clicks a link, the href change request will be aborted.  This behavior can be seen when you click one link on a page and before it changes you can click a different link and load that instead.  To avoid this problem, you could use document.write() to erase the contents of the visible frame pending loading of your page. 

Finally, behavior of the back and forward browser buttons can vary with different browsers.  I believe that most of the browsers out there will go back and forward within frames, which would work fine with the solutions presented.

#3 dark dude

dark dude
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 28 July 2006 - 03:02 PM

Ok, sounds like it will work, just two questions:

- If the user shutdown his computer, will the countdown until execution still occur on the server?

- How would you get it to work so even if you logged onto another computer, then the countdown would still be going?

#4 bltesar

bltesar
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 28 July 2006 - 03:33 PM

I'm not sure, but there are ways to test for that.  You could set a delay that is followed by an update to a database field, request the page and close your browser before the delay is up, and then check to see if the database was updated.  It is a very interesting question, please let me know if you triy this test.  I often figure things out by devising these sorts of tests because I often find it very difficult to locate the information I need in books.

As for the second question, I'm not exactly sure what you're looking to do.  One option might be to record the time of request in a database.  When the user logs onto another computer, the time is gotten from the database, compared to the current time, and then the necessary delay time can be figured. 




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users