tags:

views:

85

answers:

1

Im looking at a javascript file trying to figure out a timer issue, but im lost as to what exactly is happening here. Could someone break down this code into bite sizes and explain what is going on?

Timer=0;
function countdown(auctionid) {
    var auctions;
    var divs;

    Timer=Timer+1;

    if((Timer%10=="0")||(Timer=="1")) {
        $.get("current.php", {
            id:auctionid
        }, function(data) {
            auctions=data.split("||");
            for(n=0;n<=auctions.length;n++) {
                if(auctions[n] != undefined) {
                    divis=auctions[n].split("##");
                    $('#futu'+divis[0]).html(divis[1]);
                }
            }
        });
    }

    var cauctionid="auctionid";
    var tauctions=auctionid.split("|");
    for(i=0;i<=tauctions.length;i++) {
        if(tauctions[i] != undefined) {
            var dd=$('#futu'+tauctions[i]).text();
            var cdd=dd-1;
            $('#futu'+tauctions[i]).html(cdd);

            dd=dd*1000;
            dday=Math.floor(dd/(60*60*1000*24)*1)
            dhour=Math.floor(dd/(60*60*1000)*1)
            dmin=Math.floor((dd%(60*60*1000))/(60*1000)*1)
            dsec=Math.floor(((dd%(60*60*1000))%(60*1000))/1000*1)

            if(dday==0 && dhour==0 && dmin==0 && dsec==0) {
                $('#Bid'+tauctions[i]).html("SOLD");
                //return
            }
            if(dhour <=9) {
                dhour = "0"+dhour;
            }
            if(dmin <=9) {
                dmin = "0"+dmin;
            }
            if(dsec <=9) {
                dsec = "0"+dsec;
            }

            if(dd>=1000) {
                var valll=dhour+":"+dmin+":"+dsec;
            }

            if(dd<1000) {
                var valll="00:00:00";
            }

            $('#Bid'+tauctions[i]).html(valll);
        }
    }
    refreshID = setTimeout("countdown('"+auctionid+"')", 1000);
}
+2  A: 

Every second, this script will update the time left for each of the "auctions" on the page. The second argument to setTimeout() is the time to wait in milliseconds, thus 1000 = 1 second.

Also, on the 1st second, and every 10s afterwards, it will make an AJAX call to retrieve a set of auctions in double-pipe (||) delimited string format. It then updates the corresponding auctions on the page with the data from the server.

ken
*approximately every second* setTimeout is neither that precise, nor does the code take 0 time to execute. Also, there are numerous problems with this code starting with the Timer variable being a counter not a timer really and the logical test against string representations of what should be int literals.
annakata
He asked what it did, so I told him. Where's your answer since you seem to know it all?
ken
@annakata Of course you're right, but I guess whoever wrote that code was *not* trying to reimplement ebay. Looks more like (bad) example code. I wouldn't take it too seriously...
balpha
@ken - You said on the first second and every 10 seconds afterwards it makes an ajax call. If i wanted to randomize that amount of time, say make the call at a time between 2 and 13 seconds, how would i do that?And to answer everyones curiosity, its horrid code i know, not mine. Im trying to fix it.
Patrick