setTimeout()??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruce D

    #1

    setTimeout()??

    I have some really bizarre behavior...and I'm wondering if its because I
    don't fully understand setTimeout(). I have a web page that calls a
    function on the Onload. This function calls two separate functions and then
    uses setTimeout() to keep calling itself. Each function randomly generates
    a number and then I update the image.src with that.
    OK...it works...but here's the thing: If I run setTimeout() on just one
    function by itself, it almost always displays a new pic...but when I put the
    setTimeout() in the startup function a get a lot of duplicates. I've been
    watching it for a long time and I don't think it's a coincidence. Any idea
    as to why it seems like both functions don't run/update the pic all the
    time? If I uncomment the line, alert("hello"), it runs all the time? Hmm...

    Here's my code:

    Index.shtml:
    blah blah
    <script src="attachment s/jsfunctions.js" type="text/javascript"></script>
    </head>
    <body onLoad="preLoad (); StartPic(); GetSpecialPic() ;">
    blah blah

    jsfunctions.js:
    function GetPic()
    {
    var picversions = 7; // number of versions of pic
    var lnow = new Date();
    var ltime = lnow.getTime();
    var lrand = ((ltime % 4652353) * 492113) + 1;
    var lImg = "images/technically" + (lrand % picversions) + ".jpg";
    document.images["imgMenu"].src = lImg;
    window.defaultS tatus = lImg;
    }
    function GetMainPic()
    {
    //alert("hello");
    var mainpicversions = 2; // number of versions of pic
    var lmainnow = new Date();
    var lmainday = lmainnow.getDay () + 1;
    var lmainpic = (lmainday % mainpicversions );

    var subpicversions = 4; // number of versions of subpic
    var lnow = new Date();
    var ltime = lnow.getTime();
    var lrand = ((ltime % 4652353) * 492113) + 1;
    var lsubpic = (lrand % subpicversions) ;
    var lImg = "images/mainpic" + lmainpic + "_" + lsubpic + ".jpg";
    document.images["imgMainpic "].src = lImg;
    }

    function StartPic()
    {
    GetPic();
    GetMainPic();
    setTimeout("Sta rtPic();",3000) ;
    }


    TIA
    -bruce duncan


  • Lee

    #2
    Re: setTimeout()??

    Bruce D said:[color=blue]
    >
    >I have some really bizarre behavior...and I'm wondering if its because I
    >don't fully understand setTimeout(). I have a web page that calls a
    >function on the Onload. This function calls two separate functions and then
    >uses setTimeout() to keep calling itself. Each function randomly generates
    >a number and then I update the image.src with that.
    >OK...it works...but here's the thing: If I run setTimeout() on just one
    >function by itself, it almost always displays a new pic...but when I put the
    >setTimeout() in the startup function a get a lot of duplicates.[/color]

    [color=blue]
    > var lnow = new Date();
    > var ltime = lnow.getTime();
    > var lrand = ((ltime % 4652353) * 492113) + 1;
    > var lImg = "images/technically" + (lrand % picversions) + ".jpg";[/color]


    That's a good reason to avoid using the date to generate random numbers.
    If you call both functions onload, it.s unlikely that the two times will be
    different. Date doesn't really track time to the millisecond. That's just the
    unit that it uses for storage.

    Javascript provides a better generator, anyway:

    var lImg = "images/technically" + Math.floor(Math .random()*picve rsions) +".jpg";

    Comment

    • Bruce D

      #3
      Re: setTimeout()??

      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
      news:crkd9o013q l@drn.newsguy.c om...[color=blue]
      >
      > That's a good reason to avoid using the date to generate random numbers.
      > If you call both functions onload, it.s unlikely that the two times will[/color]
      be[color=blue]
      > different. Date doesn't really track time to the millisecond. That's[/color]
      just the[color=blue]
      > unit that it uses for storage.
      >
      > Javascript provides a better generator, anyway:
      >
      > var lImg = "images/technically" + Math.floor(Math .random()*picve rsions)[/color]
      +".jpg";

      Lee,
      Thanks for the new random generator...I'm going to test it out.
      But I think you misunderstood my problem. I don't care if it radomly pics
      the same number because if you notice the functions they update different
      pics. The problem I was having is that it wasn't updating the pics...like
      it wasn't running the code.
      Well...as it turns out...it's fixed now...and I didn't do a damn thing?!? I
      viewed my web page on another machine and notice it was loading new pics
      each time. I rebooted and BAM...it's working fine. Wierd thing...if I make
      changes to the script...someti mes...it will stop updating the pics....but
      only on my PC. I'll blam CACHE on this one...or a small gremlin!
      Anyhow...the code actually works.

      cheers to all!
      -bd


      Comment

      • Bruce D

        #4
        Re: setTimeout()??

        "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
        news:crkd9o013q l@drn.newsguy.c om...[color=blue]
        > That's a good reason to avoid using the date to generate random numbers.
        > If you call both functions onload, it.s unlikely that the two times will[/color]
        be[color=blue]
        > different. Date doesn't really track time to the millisecond. That's[/color]
        just the[color=blue]
        > unit that it uses for storage.
        >
        > Javascript provides a better generator, anyway:
        >
        > var lImg = "images/technically" + Math.floor(Math .random()*picve rsions)[/color]
        +".jpg";


        Aren't there some versions of JS that don't support Math.random()? Should I
        worry about it?

        -bruce


        Comment

        Working...