views:

178

answers:

1

I have a timer that grabs a file every 10 seconds:

if((Timer%10=="0")||(Timer=="1")){

I also have a random number generator:

var randomNum = Math.floor(Math.random() * 10) + 2;

I also have a variable which stores the time remaining:

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);
}

What I need to do is to generate a new random number after it runs, of seconds to wait until it runs again. I need this Javascript to run randomly between 2 and 12 seconds, then run again, at a new random time.

+1  A: 

I'm confused about what it is that you want to run every 2-12 seconds, and mostly the code that you have is scaring me... A function that, once called, calls itself every second forever? Scary.

It looks like you already have all of the pieces you need to answer your question. If you need to delay some javascript you can use setTimeout, and you've got random numbers (although I think your expression needs "round" rather than "floor"):

setTimeout(functionNameOrAString, Math.round(Math.random() * 10) + 2)
Prestaul