admin管理员组

文章数量:1355879

Can a body tag hold two set intervals? or have 2 functions use the same interval?

ex:

<body onload="setInterval(function1, 500); setInterval(function2, 1000);">

<body onload="setInterval(function1, function2, 500");>

Can a body tag hold two set intervals? or have 2 functions use the same interval?

ex:

<body onload="setInterval(function1, 500); setInterval(function2, 1000);">

<body onload="setInterval(function1, function2, 500");>
Share Improve this question edited Jul 12, 2011 at 7:26 leppie 117k18 gold badges200 silver badges300 bronze badges asked Jul 12, 2011 at 7:21 travistravis 72 silver badges3 bronze badges 3
  • 1 Why are people still using inline handlers instead of creating proper event listeners... – ThiefMaster Commented Jul 12, 2011 at 7:26
  • 5 The same reason people are still posting useless responses (^-^)v – travis Commented Jul 12, 2011 at 7:28
  • To answer Thief...one theory is that I haven't found a really prehensive method for that that seems truly cross-browser friendly (aside from your JS libraries like jQuery). – DA. Commented Jul 12, 2011 at 7:29
Add a ment  | 

5 Answers 5

Reset to default 6

You can create a function that calls setInterval() twice with the different functions and call it on the body.onload().

And i dont think that 2 functions can have the same interval unless you wrap them up in one or call them inline like this:

<body onload="setInterval(function(){ function1(); function2();}, 500);">

Your first example would be fine:

window.onload = function() {
   setInterval(function1, 500); 
   setInterval(function2, 1000); 
}

function function1() {
   console.log("function1");   
}

function function2() {
   console.log("function2");   
}

See an example of the above code working here.

Simply use jQuery and register an event handler (in a <script type="text/javascript"> block).

In case all you need is the DOM tree being available:

$(document).ready(function() {
    setInterval(function1, 500);
    setInterval(function2, 1000);
});

otherwise (if you need all images being loaded etc):

$(window).load(function() {
    setInterval(function1, 500);
    setInterval(function2, 1000);
});

Instantiate as many onloads as you need without the collisions.

<SCRIPT>

    // Class Definition
    OnLoad = function(taskFunction,miliseconds) { 
        var context = this;
        context.cnt = 0;
        context.id = null;
        context.doTask=taskFunction;
        context.interval = function() {
            if(document.readyState == "plete"){
               try{   context.stop();} catch(e){ throw new Error("stop error: " + context.id); }
               try{ context.doTask();} catch(e){ throw new Error("load error: " + context.id); }
            }   
        };
        context.start = function(timing) {
            if(context.id && context.id!=null)
                context.stop();
            context.cnt=0;
            context.id=setInterval(context.interval,timing);
        };
        context.stop = function() {
            var _id = context.id;
            clearInterval(context.id);
            context.id=null;
        };
        context.start(miliseconds ? miliseconds : 100);
    };


    // Example Onloads
    new OnLoad(function(){
        alert("onload 1");
    }); // uses default timing of 100 miliseconds

    new OnLoad(function(){
        alert("onload 2");
    },200);

    new OnLoad(function(){
        alert("onload 3");
    },300);

</SCRIPT>

Try This Cod it's Easy. you can run this here and TEST it.

    window.onload = function() {            
        function foo() {
           Load_Note();
        }
        setInterval(foo, 3600);}
        
        
        function Load_Note() {console.log("api loded")}

本文标签: javascriptCan a body tag hold multiply onloadquotsetInterval()quotStack Overflow