Jump to content

Unsure if I should use Javascript here...


dark dude

Recommended Posts

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...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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. 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.