About SetInterval function


I only want to ask is it possible that I set the same function at one time with SetInterval? For example :

img_ids[1] = SetInterval("makevis(obj1)",1000);
img_ids[2] = SetInterval("makevis(obj2)",400);

In this case the first function works, but if I set the second while the first runs both of them stops.

Also, can I set with SetInterval only one function or is there in my code some bug?




  • I see no reason why not, although I haven't tested it. You might get errors if you try to have a single function running at once, but since I'm pretty sure you can have recursive functions you shouldn't do. The only problem with your code that I can see is that setInterval should have a lower-case S.
  • Well, I had the time to debug and the problem was that the parameter names of the function where setInterval is called, were the same as the parameters of that function that setInterval calls.

    Exactly :

    I have an html page, there are three images. Every image has an onMouseover="makevisible(...)" and an onMouseout="makeunvisible(...)" event.
    The makevisible function calls the makevis function wich fades the image until

    filters.alpha.opacity = 100;

    The makeunvisible function calls the makeunvis function wich fades the image out til filters.alpha.opacity = 50;

    Every image has it's own id in wich setInterval returns, also there can be more fades at one time. Now it works, but not correctly. If i go with the mouse over the first image, it fades in. Than I go to the second image, also the first image fades out and the second image fades in. Also, makevisible(...) and makeunvisible(...) runs at one time, thats great! :)

    The problem comes if I quickly go with the mouse over the third image at the time the first image is fading out and the second is fading in : in this case the fading out of the firt image stops, because the onMouseout event of the second image is called.

    If I'm right than there can't be the same function set with setInterval at one time, but i hope that there is a way.

    If someone has the time there is the code :

    HTML :




    JavaScript : tools.js

    var img_ids = new Array();

    function makevisible(on_img_obj, on_alpha_max, on_in_step, on_num, on_c_ident){
    on_obj = on_img_obj; //current IMG object
    on_max = on_alpha_max; //maximum alpha to be reached
    on_step = on_in_step; //increment of alpha
    on_iden = on_c_ident; //ID number in wich setInterval returns
    on_numb = on_num; //setIntervals second parameter
    if (window.img_ids[on_iden]) clearInterval(img_ids[on_iden]);
    img_ids[on_iden]=setInterval("makevis(on_obj, on_max, on_step, on_iden)",on_numb);

    function makevis(on_cur, on_max, on_step, on_ident){
    on_c = on_cur; //current IMG object
    on_m = on_max; //maximum alpha to be reached
    on_s = on_step; //increment of alpha
    on_i = on_ident; //ID number in wich setInterval returned
    if (on_c.filters.alpha.opacityout_m)
    else if (window.img_ids[out_i])


  • [b][red]This message was edited by Weirdofreak at 2004-9-27 8:21:20[/red][/b][hr]
    Doesn't look like there's a problem with your code, so I'd guess its either the problem with setInterval not liking to share, or just inability to do many things at once. Fast mouse movements break most things that rely on timing and the mouse position, I wouldn't worry about it too much if I were you.

    One thing I would note though is that function parameters are separate from what they were called as. You don't have to assign the value to something else to avoid changing the original, you can just use them as they are, allowing you to cut out the four or five lines at the beginning of the function.

    Oh, and you also may want to consider using style.opacity and style.-moz-opacity as well as filters.alpha or whatever. The filters are IE only, but opacity works on Safari and is going to become more widely used when CSS3 comes out, and -moz-opacity is exactly the same as opacity, but the Mozilla foundation put the -moz- on the front because it isn't yet a W3C recommendation. It only works in Mozilla, Firefox and possibly other Gecko-based browsers.
  • Thanks a lot :)

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!