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 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    55
    Thanks
    18
    Thanked 0 Times in 0 Posts

    some javascript that drive me crazy

    Code:
    <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;
    http.send(null);
    }
    
    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;
    countDown()
    }
    } else {
    //document. getElementById('actual').innerHTML = "";
    }
    }
    
    function countDown() {
    countDownTime--;
    if (countDownTime == 0) {
    countDownTime = countDownInterval;
    getServerText()
    }
    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);
    stopCountDown();
    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()
    {
    clearTimeout(counter)
    }
    
    </script>

    html part

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



    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

    Maxwel
    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
    Location
    Sydney, Australia
    Posts
    6,454
    Thanks
    0
    Thanked 632 Times in 622 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.
    Stephen
    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
    Posts
    55
    Thanks
    18
    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
    •