TeddyKiller Posted March 12, 2010 Share Posted March 12, 2010 I'm looking for a script that is likely to.. not refresh the page. but sometihng like.. every X seconds { do this } How is it possible? I'll accept PHP or Javascript/Ajax Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/ Share on other sites More sharing options...
FD_F Posted March 12, 2010 Share Posted March 12, 2010 in this case use CRON JOB (if no user interface involve ) Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025386 Share on other sites More sharing options...
TeddyKiller Posted March 12, 2010 Author Share Posted March 12, 2010 I've never used CRON, can you help me out? I'm needing to include php queries.. I basically need to update some things in the table every 10 seconds basically. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025390 Share on other sites More sharing options...
trq Posted March 12, 2010 Share Posted March 12, 2010 I basically need to update some things in the table every 10 seconds basically. You don't need php for that then. You just execute mysql queries within a cron job. If your having problems with Cron, your question belongs in Linux help. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025399 Share on other sites More sharing options...
Tazerenix Posted March 12, 2010 Share Posted March 12, 2010 you could use jQuery AJAX to load a page with the relevant information into the table every ten seconds. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025401 Share on other sites More sharing options...
TeddyKiller Posted March 12, 2010 Author Share Posted March 12, 2010 you could use jQuery AJAX to load a page with the relevant information into the table every ten seconds. Would that mean it'd be reloading the page? Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025418 Share on other sites More sharing options...
Tazerenix Posted March 12, 2010 Share Posted March 12, 2010 no. AJAX uses XMLHttpRequests so it would only reload the Table. Not the whole page Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025420 Share on other sites More sharing options...
TeddyKiller Posted March 12, 2010 Author Share Posted March 12, 2010 oh no no, I don't mean a <table></table> I mean a database table. It needs to update it every 10 seconds. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025421 Share on other sites More sharing options...
trq Posted March 12, 2010 Share Posted March 12, 2010 Your going to need to use cron unless you want to leave a browser window open and executing this ajax. Oh, yeah, and by the way. It is possible (and very often done) to execute database queries using Ajax. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025422 Share on other sites More sharing options...
Tazerenix Posted March 13, 2010 Share Posted March 13, 2010 of course it is. jQuery ajax will allow you to load a php page into a division of your page. Inside that php page just put your queries and the layout of your table Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025425 Share on other sites More sharing options...
trq Posted March 13, 2010 Share Posted March 13, 2010 The big questions here though. Do you want these queries to execute every 10 seconds, even when there is no one on the site? Use Cron. Do you want these queries to execute every 10 seconds while someone views a page? Use Ajax.. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025428 Share on other sites More sharing options...
TeddyKiller Posted March 13, 2010 Author Share Posted March 13, 2010 It'll be the AJAX one, any idea how it's done? Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025432 Share on other sites More sharing options...
Tazerenix Posted March 13, 2010 Share Posted March 13, 2010 regardless. If no1 is viewing the site they dont need to be updated every ten seconds. Just set it so on pageload it loads the php page with the queries so it will update when a user goes on the site, or every 10 seconds Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025434 Share on other sites More sharing options...
trq Posted March 13, 2010 Share Posted March 13, 2010 The Javascript. function updateTable() { $.load('update.php'); } setInterval( "updateTable()", 10000 ); You'll also need jquery which you get by adding the following line to the <head> of your document. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"><?script> Then just do whatever needs doing within update.php Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025435 Share on other sites More sharing options...
trq Posted March 13, 2010 Share Posted March 13, 2010 regardless. If no1 is viewing the site they dont need to be updated every ten seconds. You don't have enough information to be able to state that as a fact. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025437 Share on other sites More sharing options...
Tazerenix Posted March 13, 2010 Share Posted March 13, 2010 unless the server needs to be acting on the data all the time you dont really need to. But i suppose if you really needed too, something like a search engine that updates its searches or something Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025439 Share on other sites More sharing options...
trq Posted March 13, 2010 Share Posted March 13, 2010 unless the server needs to be acting on the data all the time you dont really need to. No kidding. I'm just saying you didn't have enough information to say what you did. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025441 Share on other sites More sharing options...
Tazerenix Posted March 13, 2010 Share Posted March 13, 2010 fair enough Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025442 Share on other sites More sharing options...
TeddyKiller Posted March 13, 2010 Author Share Posted March 13, 2010 It doesn't work, what I'm assuming, when the echo comes it's not starting the function. Can you help? <?php include("lib.php"); define("PAGENAME", "Resting"); $player = check_user($secret_key, $db); ?> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> <script type="text/javascript"> function updateTable() { $.load('enhp.php'); alert("You healed 1HP and 1Energy point!"); } </script> </head> <?php echo '<center>'; if ($player->hp == $player->maxhp && $player->energy == $player->maxenergy) { echo "<i>You have full HP and Energy.</i>\n"; exit; } else { if ($_GET['act'] == "rest") { echo '<script type="text/javascript">setInterval("updateTable()",12000);</script>'; echo 'You are now resting. You will gain 1hp and 1energy point every 5 seconds. To unrest type "unrest"'; } } echo '</center>'; ?> Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025614 Share on other sites More sharing options...
Tazerenix Posted March 13, 2010 Share Posted March 13, 2010 <html> <?php include("lib.php"); define("PAGENAME", "Resting"); $player = check_user($secret_key, $db); ?> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script> <script type="text/javascript"> function updateTable() { $("#resting").load('enhp.php'); alert("You healed 1HP and 1Energy point!"); } </script> </head> <body> <?php echo '<center>'; if ($player->hp == $player->maxhp && $player->energy == $player->maxenergy) { echo "<i>You have full HP and Energy.</i>\n"; exit; } else { if ($_GET['act'] == "rest") { echo '<div id="resting"></div>'; echo '<script type="text/javascript">setInterval("updateTable()",12000);</script>'; echo 'You are now resting. You will gain 1hp and 1energy point every 5 seconds. To unrest type "unrest"'; } } echo '</center>'; ?> </body> </html> That should work. Your problem was that the .load jQuery function requires a selector to load into. I've created the div resting. If enhp.php outputs any text it will be loaded into #resting. But as enhp is just getting database querys (isnt it?) it shouldnt do anything. But that should work. Link to comment https://forums.phpfreaks.com/topic/195062-do-something-every-x-seconds/#findComment-1025747 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.