SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2001
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    random click javascript question

    i have a javascript code that enables me to generate a clickable image that will display preloaded random images when clicked.

    my question is: can the clicking of the image happen only once? is there a way to stop it from being perpetually clicked?
    what?!?

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    without seeing any code....

    create a global variable; before loading the image, check the variable, if it's value is not the same as you gave it, do nothing, else reset the variable and load the image

    var didThisBefore = false;
    if (didThisBefore)
    else
    {
    didThisBefore = true;
    ...load image...
    }

    or you can load the image according to the time (see the Read Banner Data script/tutorial at my site) when the page is accessed -- then you won't need a clickable image

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Member
    Join Date
    Dec 2001
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks! the code goes like this:

    <!-- Begin
    var rand1 = 0;
    var useRand = 0;

    images = new Array;
    images[1] = new Image();
    images[1].src = "image1.gif";
    images[2] = new Image();
    images[2].src = "image2.gif";
    images[3] = new Image();
    images[3].src = "image3.gif";
    images[4] = new Image();
    images[4].src = "image4.gif";
    images[5] = new Image();
    images[5].src = "image5.gif";

    function swapPic() {
    var imgnum = images.length - 1;
    do {
    var randnum = Math.random();
    rand1 = Math.round((imgnum - 1) * randnum) + 1;
    } while (rand1 == useRand);
    useRand = rand1;
    document.randimg.src = images[useRand].src;
    }
    // End -->


    however, for the site we're creating, i have 53 images on the array. the image is perpetually clickable basing from the code above. what the client wants is, when you click on the default image, it will randomize the array and display an output. the new image will not be clickable anymore.
    what?!?

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so...

    function swapPic()
    {
    if (didthisbefore) return
    ........

    didthisbefore = true;
    }

    where do i send the bill?
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    SitePoint Member
    Join Date
    Dec 2001
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    excuse me for being illiterate D), but is didthisbefore a built-in code or does it have to be declared first?
    what?!?

  6. #6
    SitePoint Member
    Join Date
    Dec 2001
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i tried it but it won't work. i got an Error on Page instead.

    i inserted the code as you placed them above, it went like this:

    function swapPic() {
    if (didthisbefore) return
    var imgnum = images.length - 1;
    do {
    var randnum = Math.random();
    rand1 = Math.round((imgnum - 1) * randnum) + 1;
    } while (rand1 == useRand);
    useRand = rand1;
    document.randimg.src = images[useRand].src;
    didthisbefore = true;
    }


    did i get it right?
    Last edited by ralphot; Jan 28, 2002 at 17:11.
    what?!?

  7. #7
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    didthisbefore isn't built-in; it's a var; if you look up to my first post, you'll see:

    var didthisbefore = false;

    that creates a variable and sets it value to false; you can rename it to anything you like

    within the swapPic() function, the first think you do is see if that value was set to true.

    if (didthisbefore) return

    and if it isn't, you perform your swap and then reset the value to true, so that swapPic won't swap any more pictures:

    didthisbefore = true;

    now, if the error you got had nothing to do with 'didthisbefore' (which I doubt because the error was probably something like: var is undefined), you will have to post the html code and the error message.

    Hope this helps and clears any confusion

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

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