Kristian_C Posted January 11, 2007 Share Posted January 11, 2007 hey ppl. anyone here that knows what the best way to make a countdown timer based on a time thats was inserted to sql? like with the normal time() function, its for a spam stopper so ppl must wait 30-40 seconds before they can send a new message... and i want it to be live countdown not just a timer that changes when you update page..anyone that can help me with this. IF the script must be js pleace explain it good so i dont copy and paste without learning. well my countdown function is like this but is it not live : function mekktid($last){$tidno = time(); if($last>$tidno){ $ord = $last-$tidno; while($ord >= 60){ $ord = $ord-60; $ordleft++; } while($ordleft >= 60){ $ordleft = $ordleft-60; $ordlefth++; } if($ordleft == 0){$ordleft = "";} else {$ordleft = "$ordleft Minutes"; } if($ordlefth == 0){ $ordlefth = ""; } else { $ordlefth = "$orderhleft Hours"; }return "$ordlefth $ordleft $ord seconds";}} ->Kristian_C Link to comment Share on other sites More sharing options...
Little Goat Posted January 11, 2007 Share Posted January 11, 2007 you would have to use Javascript to update the page, you could use setTimeout(functiontochangetime(), 1000);that would execute the function after that amount of time (in milliseconds), and you would put that in the end of functiontochangetime() so that it would do it every second. then you could either use ajax to get the time from a php page, or you could just set a span's innerHTML to spansid.innerHTML-1 and if it was zero, redirect to a different page.of course you might have to log their IP and put a time on that so they won't just press back and do it over again. Link to comment Share on other sites More sharing options...
jesh Posted January 11, 2007 Share Posted January 11, 2007 I think a server-side solution would be best for this. You could use javascript on the client to prevent people from clicking a submit button until a certain amount of time elapsed, but if someone disabled (or somehow circumvented) the javascript, the form would be submitted and you'd want to check it on the server to see if there was enough time elapsed.I would recommend accepting the form submission, and then, on submit, getting the current server time and comparing it with the last-message time that is stored in your database. If the difference between those two times is less than some pre-determined amount, direct the user back to the form submission page with a message informing him/her that s/he needs to wait before submitting a new message. Link to comment Share on other sites More sharing options...
Little Goat Posted January 11, 2007 Share Posted January 11, 2007 yeah, that would be the best way, but if you wanted the number to change as you go, then you would need JS to display it on the page. Link to comment Share on other sites More sharing options...
Kristian_C Posted January 11, 2007 Author Share Posted January 11, 2007 I have the block thing thats just : if($fetch->lastmess > time()){ echo"You need to wait ".mekktid($fetch->lastmess)."";exit(); So thats not the problem. But i still want the user to be able to see the page all the time with a live countdown. And since i am a neewbie in ajax/js i cant make a function that updates without beeing refreshed so the people can see it, only thing i can refresh with is by useing the meta function...so if you can help with the js/ajax function that whould be great ->Kristian_C. Link to comment Share on other sites More sharing options...
jesh Posted January 11, 2007 Share Posted January 11, 2007 What if you looked up the last-message time before you sent the page in the response and stored that date/time in a hidden field on the form. <script type="text/javscript">// First, get the time from the hidden value and parse it as a javascript datevar lastMessageTime = new Date(document.getElementById("lastMessageTime").value);// some global variablesvar now, timeElapsed, timeLeft;// this is the predeterminied number of minutes a person has to waitvar minWaitTime = 40; // 40 minutes// This function calculates the time remaining and displays it on the screen.function updateCountdown(){ // first, get the new current time. now = new Date(); // next, figure out the new timeElapsed. We have to divide by // 60,000 because there are 60,000 milliseconds in a minute // and when you subtract dates, you get the difference in milliseoconds var timeElapsed = (now - lastMessageTime) / 1000 / 60; // next, figure out how many minutes they have left to wait timeLeft = minWaitTime - timeElapsed; if(timeLeft > 0) { // let's update the message. document.getElementById("countdown").innerHTML = timeLeft + " minutes left to wait!"; // and set a timeout to call this function again in a minute setTimeout("updateCountdown();", 60000); }}// finally, we call the updateCountdown function for the first time:updateCountdown();</script> Link to comment Share on other sites More sharing options...
Little Goat Posted January 11, 2007 Share Posted January 11, 2007 so what you would do on the page you want people to see is this: <script type="text/javascript">function changeTime(Span){Content=Span.innerHTML;Content-=1;if(Content<=0){ Content="You may submit again."; Span.innerHTML=Content;}else{ Span.innerHTML=Content; setTimeout(changeTime(Span), 1000); }}</script> and put this wherever you want the time to show up: <span id="time"><?php echo $fetch->lastmess - time(); ?></span> then at the bottom of you page, right before the </body> tag, put this: <script>changeTime(document.getElementById('time'));</script> EDIT: oh, I guess I type too slow. Link to comment Share on other sites More sharing options...
Kristian_C Posted January 11, 2007 Author Share Posted January 11, 2007 Thanks guys Just need to wait till i get my server back before i can test it if i get any errors or problems ill just post again Thanks again ->Kristian_C. Link to comment Share on other sites More sharing options...
justsomeguy Posted January 11, 2007 Share Posted January 11, 2007 The setTimeout function that Little Goat keeps showing will execute a piece of code after a certain amount of time. There's a small bug though, the code to execute needs to be in quotes. So this line:setTimeout("changeTime(Span)", 1000);Will execute the changeTime(Span) function every 1000 milliseconds, or once a second. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now