Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-01-2012, 12:24 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
countdown or countup redirect

Hi, i have a limit i am setting on my chatroom they have a certain mount of time to chat (i have a good reason)

anyway so i have the time set in a var

I post the var on the page "you have this much time" (its depending on the member status)

I got a redirect script but it just does seconds, then i found a few countdown script but all of them had issues either the seconds didnt work or when the seconds go to 09 it froze.

Anyway long story short i been at this for 6 hours now and i cannot seem to find a good script that i can load a value hour min sec into and have it countdown.

or load hour min sec into and have it count from zero to that amount.

i found phils on a thread here but again it only does seconds.

I have tried to take the code from one and put it into another but im totally upside down now. lol

what i start with is this 00:00:00 h m s and i get that by

PHP Code:

//creditworth_sec  is how many total seconds

$chatallowed gmdate("H:i:s"$creditworth_sec); 

my plan is to redirect when the time is up either count up or count down

thanks




UPDATE i ripped it all out and starting over. I know that phils works, ty phill so what i might do is just use his to count down 60 sec at a time on the screen.
then every loop add one to a counter and display that total on the screen in a div, then when that total equals my total do the redirect. i dont do partial min so it will be full min.

how does that sound, to me it sounds so much less complicated than calculating seconds and converting and all that.


UPDATE again lol. Ok i like that i idea forget the original id.

here is what i have i like this much better. ill be back if i need help and phil thanks again nice job ..

PHP Code:

<script type="text/javascript">

/*author Philip M. 2010*/

var minutes=0;
var 
timeInSecs;
var 
ticker;

function 
startTimer(secs){
timeInSecs parseInt(secs)-1;
ticker setInterval("tick()",1000);   // every second
}

function 
tick() 
{
 var 
secs timeInSecs;
  if (
secs>0
  {
  
timeInSecs--;
  }else {
        
clearInterval(ticker); // stop counting at zero
        
startTimer(60);  // remove forward slashes in front of startTimer to repeat if required
        
minutes minutes 1;
        }
//close else

 
 
document.getElementById("countdown").innerHTML secs;
 
document.getElementById("minutes").innerHTML minutes;

}
//close function tick

</script> 

Last edited by durangod; 12-01-2012 at 01:32 PM..
durangod is offline   Reply With Quote
Old 12-01-2012, 02:45 PM   PM User | #2
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
Numerical strings beginning by 0 are interpreted as hexadecimal integers, then you have to write intSec=parseInt(strSec,10); to transform such a chain to an integer.

Since see this tread from web.developers which contains a very good count-down timer

Last edited by 007julien; 12-01-2012 at 03:32 PM..
007julien is offline   Reply With Quote
Old 12-01-2012, 03:14 PM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
Thanks for the link. Right now im getting an undefined in the minute var

i dont know why its showing

undefined60

then it goes to

Time Used
NaN26

the seconds is counting fine but, i dont understand what is undefined or i would fix it lol

PHP Code:
<script type="text/javascript">

var 
limit document.redirect.limit.value;
var 
targURL="/chatcomplete.php";
var 
minutes=0;
var 
timeInSecs;
var 
ticker;

function 
startTimer(secs)
{
timeInSecs parseInt(secs)-1;
ticker setInterval("tick()",1000);   // every second
}

function 
tick() 
{
 var 
secs timeInSecs;
  if (
secs>0
  {
  
timeInSecs--;
  }else {
        
clearInterval(ticker); // stop counting at zero
        
startTimer(60);  // remove forward slashes in front of startTimer to repeat if required
        
minutes minutes 1;
  
        
/* if(minutes == limit)
        {
         window.close(); //this should close the chat window since it is the most current window, it is not named.
         //do some other stuff here too 
        } */

      
}//close else 
durangod is offline   Reply With Quote
Old 12-01-2012, 03:37 PM   PM User | #4
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
I suppose you call tick() to begin ?
then timeInsecs as no value secs is not bigger than 0...

NB:ticker = setInterval(tick,1000); is better than ticker = setInterval("tick()",1000);
007julien is offline   Reply With Quote
Old 12-01-2012, 03:45 PM   PM User | #5
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
no i call it by


Code:
startTimer(60);
i even added this

Code:
timeInMins = parseInt(minutes);

and then this in tick

var minutes = timeInMins;
still get NaN

it was working before i dont get js sometime, seems to make the simplist things the hardest sometimes..

Here is the whole thing form and all as it is, i commented our some stuff to try to trouble shoot

PHP Code:

<em>Time Used</em><br />
<span id="minutes">00</span>
<span id="countdown">60</span>


<?php

//now split time up into array so you can pass min to js
//this will be passed to form and  js for the limit var
$alow_array explode(":",$chatallowed);
$alhour $alow_array[0]; //hours 
$almin $alow_array[1]; //min

?>

<script type="text/javascript">

/* var limit = document.redirect.limit.value; */
/* var targURL="/chatcomplete.php"; */
var timeInSecs;
var ticker;

function startTimer(minutes,secs)
{
timeInSecs = parseInt(secs)-1;
timeInMins = parseInt(minutes);
ticker = setInterval("tick()",1000);   // every second
}

function tick() 
{
 var minutes = timeInMins;
 var secs = timeInSecs;
  if (secs>0) 
  {
  timeInSecs--;
  }else {
        clearInterval(ticker); // stop counting at zero
        startTimer(60);  // remove forward slashes in front of startTimer to repeat if required
        var minutes = minutes + 1;
  
        /* 
         if(minutes == limit)
         {
         window.close(); //this should close the chat window since it is the most current window, it is not named.
         window.location=targetURL;
         return;
         //do some other stuff here too 
        } 
        */

      }//close else

 
 document.getElementById("countdown").innerHTML = secs;
 document.getElementById("minutes").innerHTML = minutes;
}//close function tick



</script>

         
<center>
<form name="redirect" id="redirect" action="javascript:popUp('avchat/index.php');"> 
<input type="hidden" name="limit" id="limit" value="<?=$almin;?>" /> 
<input type="submit" name="submit" value="Start Chatting" class="button" onclick="startTimer(60);" />
</form>

<form name="frmstop" method="post" action="chat_complete.php">
<input type="submit" class="button" name="stopchat" value="Stop Chatting" />
</form>
</center>
durangod is offline   Reply With Quote
Old 12-01-2012, 03:55 PM   PM User | #6
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
i went back to the one in my first post, bottom that actually works, then i will trouble shoot as i go. thanks...

I just need to add the limit as well as close it all down when i add the redirect, but i will take it one step at a time. again thanks
durangod is offline   Reply With Quote
Old 12-01-2012, 04:37 PM   PM User | #7
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
ok quick question, im almost there wheeww lol

how do i pass the value of incremented minutes to the form

i want to pass

Code:
<span id="minutes">00</span>:
to

Code:
<input type="hidden" class="button" name="used" id="used" value="<?=$used;?>" />

maybe like this, is this how?

Code:
document.getElementById("used").innerHTML=minutes;
durangod is offline   Reply With Quote
Old 12-01-2012, 04:46 PM   PM User | #8
007julien
Regular Coder

 
Join Date: May 2012
Location: France
Posts: 115
Thanks: 0
Thanked 17 Times in 15 Posts
007julien is an unknown quantity at this point
Do not use minutes and seconds, but only timeInSecs and call startTimer with minutes and seconds with something like this startTimer(0,60) or startTimer(1,0) (for 1 minute)
Code:
var timeInSecs;
var ticker;

function startTimer(minutes,secs){
   timeInSecs = minutes*60+secs-1;
   ticker = setInterval(tick,1000);   // every second
}

function tick() {
 var minutes = Math.floor(timeInSecs/60);
 var secondes = timeInSecs%60;
  if (timeInSecs>0) {
  timeInSecs--;
  }else {
        clearInterval(ticker); // stop counting at zero
        startTimer(60);  // remove forward slashes in front of startTimer to repeat if required
        var minutes = minutes + 1;
  
        /* 
         if(minutes == limit)
         {
         window.close(); //this should close the chat window since it is the most current window, it is not named.
         window.location=targetURL;
         return;
         //do some other stuff here too 
        } 
        */

      }//close else

 
 document.getElementById("countdown").innerHTML = secondes;
 document.getElementById("minutes").innerHTML = minutes;
}//close function tick

Last edited by 007julien; 12-01-2012 at 04:50 PM..
007julien is offline   Reply With Quote
Users who have thanked 007julien for this post:
durangod (12-01-2012)
Old 12-01-2012, 04:51 PM   PM User | #9
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
got it

Code:
document.getElementById('used').innerHTML=minutes;
does it make any difference if i use single quotes around input name or double quotes?

and on this part below, do i need the return if i am closing everything out and redirecting?

Code:
if(minutes >= limit)
         {
         window.close(); //this should close the chat window since it is the most current or top window, it is not named.
         window.location=targURL;
         return;
         //do some other stuff here too 
        }

Last edited by durangod; 12-01-2012 at 04:57 PM..
durangod is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:11 PM.


Advertisement
Log in to turn off these ads.