Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2012
    Thanked 0 Times in 0 Posts

    some javascript that drive me crazy

    <script language="JavaScript">
    var http = getXMLHTTPRequest();
    var counter;
    function getXMLHTTPRequest() {
    try {
    req = new XMLHttpRequest();
    } catch(err1) {
    try {
    req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
    req = false;
    return req;
    function getServerText() {
    var myurl = 'http://mywebsitepathtosamphpweb/display/aj_TimeRemain.php'; // enter the full path to the aj_TimeRemain.php path
    myRand = parseInt(Math.random()*999999999999999);
    var modurl = myurl+"?rand="+myRand;
    http.open("GET", modurl, true);
    http.onreadystatechange = useHttpResponse;
    function useHttpResponse() {
    if (http.readyState == 4) {
    if(http.status == 200) {
    // parse data returned from aj_TimeRemain.php
    var aj_results = http.responseText.split("|");
    var aj_duration = aj_results[0];
    var aj_secsremaining = aj_results[1];
    //set seconds remaining to the countdown car - adding '-0' to convert to number
    countDownInterval = aj_secsremaining - 0;
    countDownTime = countDownInterval + 1;
    } else {
    //document. getElementById('actual').innerHTML = "";
    function countDown() {
    if (countDownTime == 0) {
    countDownTime = countDownInterval;
    else if (countDownTime < 0)
    countDownTime =30;
    if (document.all)
    document.all.countDownText.innerText = secsToMins(countDownTime);
    else if (document.getElementById)
    document.getElementById("countDownText").innerHTML = secsToMins(countDownTime);
    counter = setTimeout("countDown()", 1000);
    function secsToMins(theValue) {
    var theMin = Math.floor(theValue / 60);
    var theSec = (theValue % 60);
    if (theSec < 10)
    theSec = "0" + theSec;
    return(theMin + ":" + theSec);
    function stopCountDown()

    html part

    <body onLoad="getServerText()">
    <div id="countDownLabel"><font color = #00FF00>Time Left:</div> <div id="countDownText"> loading</div></font>

    Basically this script is achieved using Javascript and PHP.
    The PHP code would grab the timestamp when the currently playing track started and it's duration from the database.
    Then you take javascript and calculate the elapsed time by comparing NOW to the timestamp.
    Subtract that from the full duration and you get the remaining time and then build a countdown or take any of the bazillion snippets on the interwebs doing that.

    i grab timestamp from the database well by another php script but i am sure the problem is related to the js and i am not that good with js so i wanted to ask if someone pro can help. the result from this code is that the counter keeps counting down from 30 to 0 then show some random number like "58:29-17" << something like that but thats should not be the output. it should be mins and secs counter. any idea?

    Thanks in advance

    Last edited by VIPStephan; 01-09-2014 at 06:07 PM. Reason: restored original post

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 634 Times in 624 Posts
    language="JavaScript" is long dead (around Netscape 3 was when it was removed).

    document.all is long dead (only ever needed for Internet Explorer 4).

    Those activeX objects for the server call were only ever needed for IE4 through 6 - also now long dead.

    You are using parseInt (converts between number bases) when you should be using Math.floor (round down to nearest integer).

    You are passing a string instead of a function to setTimeout - only needed for IE3.

    If you put the JavaScript just before the </body> tag where JavaScript belongs then you can get rid of the onload and run the script straight away.

    Making those changes will get rid of about half the code and make it easier to see what is going on with the half you do need.
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Jan 2012
    Thanked 0 Times in 0 Posts
    fixed it thanks alot


    Posting Permissions

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