Twist3d Posted September 5, 2010 Share Posted September 5, 2010 Hello. Well i'm trying to make a timer to execute some code. Basically I want 3 minutes, every 3 minutes a piece of code is activated, then another 3 minutes later it does so again. But I have tried different ways of doing this, I tried with cookies and found out the person with the cookie is the only one that can refresh the page to activate the code. Same with sessions. And all the others are usually interrupted and start the timer again when the page reset. So yeah, I don't want it to be interrupted by refreshing. So any help please? I also tried with timestamps, this has proven to be the best way but I can never get it working 100% Quote Link to comment Share on other sites More sharing options...
freeloader Posted September 5, 2010 Share Posted September 5, 2010 You want that timer to run on one page and execute code every x seconds? This is how you can start a timer: $mtime = microtime(); $mtime = explode(' ', $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; Then every x seconds check: $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = round($endtime - $starttime); If total time >= 180, execute next piece of code. This would require you to keep the page open and output the results in it while it's loading (flushing). You'd need following parameters for that: ob_implicit_flush(); set_time_limit(0); That would make the script work like you want to. But it's terrible coding practice. A better way would be to have a static page that fully loads but with AJAX functionality. Store your stuff in the database and let ajax execute a php script that compares the timestamps and execute code when necessary then output back to the browser. There's plenty of tutorials about ajax, and I've written a simple ajax code for someone else yesterday as well so you might find some inspiration there: http://www.phpfreaks.com/forums/index.php/topic,309154.0.html Quote Link to comment Share on other sites More sharing options...
Twist3d Posted September 5, 2010 Author Share Posted September 5, 2010 You want that timer to run on one page and execute code every x seconds? This is how you can start a timer: $mtime = microtime(); $mtime = explode(' ', $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; Then every x seconds check: $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = round($endtime - $starttime); If total time >= 180, execute next piece of code. This would require you to keep the page open and output the results in it while it's loading (flushing). You'd need following parameters for that: ob_implicit_flush(); set_time_limit(0); That would make the script work like you want to. But it's terrible coding practice. A better way would be to have a static page that fully loads but with AJAX functionality. Store your stuff in the database and let ajax execute a php script that compares the timestamps and execute code when necessary then output back to the browser. There's plenty of tutorials about ajax, and I've written a simple ajax code for someone else yesterday as well so you might find some inspiration there: http://www.phpfreaks.com/forums/index.php/topic,309154.0.html Hmm, Ill try that code first just before I get into Ajax, I'm only beginning working with timers and time and stuff in PHP so So something like this should suffice? Or am I an idiot lol. <?php $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = round($endtime - $starttime); ob_implicit_flush(); set_time_limit(0); if ( $totaltime >= "180" ) { echo "Done"; } ?> Quote Link to comment Share on other sites More sharing options...
Twist3d Posted September 5, 2010 Author Share Posted September 5, 2010 I tried that out and it didn't work. any help? Quote Link to comment Share on other sites More sharing options...
rwwd Posted September 5, 2010 Share Posted September 5, 2010 Hi all, Sounds like your wanting your code minus the timer in a file, pop it on your server & then set up a cron job - that would be the best solution, unless this isn't on a server and your on localhost in which case, ignore me Cheers, Rw Quote Link to comment Share on other sites More sharing options...
Twist3d Posted September 5, 2010 Author Share Posted September 5, 2010 Hi all, Sounds like your wanting your code minus the timer in a file, pop it on your server & then set up a cron job - that would be the best solution, unless this isn't on a server and your on localhost in which case, ignore me Cheers, Rw This isn't on a server lol. But for the first bit, no, I want the time to execute code both in the same file kind of like the above code. Quote Link to comment Share on other sites More sharing options...
fortnox007 Posted September 5, 2010 Share Posted September 5, 2010 Thx freeloader for you explanation and tutorial, I still have to learn ajax, so i could use all the help I can get Cheers! Quote Link to comment Share on other sites More sharing options...
Twist3d Posted September 6, 2010 Author Share Posted September 6, 2010 Can anyone help me? Quote Link to comment Share on other sites More sharing options...
Twist3d Posted September 13, 2010 Author Share Posted September 13, 2010 Sorry for the gravedig but Id really appreciate it if someone could help as I do need this. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted September 14, 2010 Share Posted September 14, 2010 Are you trying to create a PHP function that works like JavaScripts setInterval or setTimeout methods? If so you'll have to look into shared memory and pcntl_fork(). If you're trying to do this on Windows I recommend a different programming language. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.