Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2002
    Location
    nyc
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy .js math problem and counting down

    heres a script i pasted together from other sources; it works ( yes isnt that amazing) but what i need is a way to convert the seconds to minutes and seconds to be displayed to the user. i know its done with math but i cant really figure it out in .js

    can someone PLEASE help me to do this?
    besides you get the used of a wonderful timed redirect script thats not embedded in a form and is cross browser tested.


    heres the script

    <script language="JavaScript">

    //Redirect without form-tag page script

    //change below target URL to desired place
    var targetURL="http://www.cnn.com"
    //configure redirect time (in seconds)
    var countDownInterval=1800;
    //configure width of displayed text, in px (applicable only in NS4.x)
    var c_redirectwidth=200

    </script>


    <ilayer id="c_redirect" width=&{c_redirectwidth}; ><layer id="c_redirect2" width=&{c_redirectwidth}; left=0 top=0 z-index:3></layer></ilayer>

    <script>

    var countDownTime=countDownInterval+1;
    function countDown(){
    countDownTime--;
    if (countDownTime <=0){
    countDownTime=countDownInterval;
    clearTimeout(counter)
    window.location=targetURL
    return
    }
    if (document.all) //if IE 4+
    document.all.countDownText.innerText = countDownTime+" ";
    else if (document.getElementById) //else if NS6+
    document.getElementById("countDownText").innerHTML=countDownTime+" "
    else if (document.layers){ //TEXT FOR NS4.x
    document.c_redirect.document.c_redirect2.document.write('Next redirect in <b id="countDownText">'+countDownTime+' </b> seconds')
    document.c_redirect.document.c_redirect2.document.close()
    }
    counter=setTimeout("countDown()", 1000);
    }

    function startit(){
    if (document.all||document.getElementById) //TEXT FOR IE
    document.write('Next redirect in <b id="countDownText">'+countDownTime+' </b> seconds')
    countDown()
    }

    if (document.all||document.getElementById)
    startit()
    else
    window.onload=startit

    </script>
    <!--end countdown script -->

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here ya go. This is your code with the changes highlighted.


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
    <title>Page title</title>
    </head>
    <body>

    <script language="JavaScript">

    //Redirect without form-tag page script

    //change below target URL to desired place
    var targetURL="http://www.cnn.com"

    //configure redirect time (in seconds)
    var countDownInterval=1800;

    //configure width of displayed text, in px (applicable only in NS4.x)
    var c_redirectwidth=200

    </script>


    <ilayer id="c_redirect" width=&{c_redirectwidth}; ><layer id="c_redirect2" width=&{c_redirectwidth}; left=0 top=0 z-index:3></layer></ilayer>

    <script>

    var countDownTime=countDownInterval+1;

    function countDown(){
    countDownTime--;
    if (countDownTime <=0){
    countDownTime=countDownInterval;
    clearTimeout(counter)
    window.location=targetURL
    return
    }
    if (document.all) //if IE 4+
    document.all.countDownText.innerText = secToMin(countDownTime)+" ";
    else if (document.getElementById) //else if NS6+
    document.getElementById("countDownText").innerHTML=secToMin(countDownTime)+" "
    else if (document.layers){ //TEXT FOR NS4.x
    document.c_redirect.document.c_redirect2.document.write('Next redirect in <b id="countDownText">'+secToMin(countDownTime)+' </b> seconds')
    document.c_redirect.document.c_redirect2.document.close()
    }
    counter=setTimeout("countDown()", 1000);
    }

    function startit(){

    if (document.all||document.getElementById) { //TEXT FOR IE
    document.write('Next redirect in <b id="countDownText">' + secToMin(countDownTime) +' </b> seconds');
    countDown();
    }
    }

    function secToMin (theseconds) {
    var theTime = new String();
    var minutes = Math.floor (theseconds /60);
    var seconds = theseconds % 60;

    theTime = minutes + " Minutes " + seconds;
    return theTime;
    }


    if (document.all||document.getElementById)
    startit()
    else
    window.onload=startit

    </script>
    <!--end countdown script -->


    </body>
    </html>
    Last edited by RadarBob; 07-11-2002 at 08:40 PM.

  • #3
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,460
    Thanks
    1
    Thanked 23 Times in 21 Posts
    Hm. Well, the code itself is probably a bit bloated, in my opinion. The best cross-browser solution would use a form text input as the place to store the counter.

    It looks like the countDownTime variable is where the actual number of seconds is stored.

    So:

    var minutes = Math.floor(countDownTime / 60)
    var seconds = countDownTime % 60

    That should do it. The % operator is called a "mod" operator, which gives you the remainder after dividing (by 60, in this case).

    EDIT: Hehe, I must be losing my touch.
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #4
    New to the CF scene
    Join Date
    Jul 2002
    Location
    nyc
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    THANKYOU!!!

    thanks so much ; your change really did the trick!!


    (one day i wanna grow up and be just like you guys!)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •