admin管理员组

文章数量:1429785

Pretty much, my question is simple, and I did search for a similar question before posting this. I would like to know how to make Javascript code repeatedly execute while the page is open, rather than the code running once and being done or only responding to event handlers. I pretty much want the Javascript equivilant of:

$(document).ready(function() {

});

But I do not want to use Jquery because it is less efficient. I want to check an === condition every single frame.

Pretty much, my question is simple, and I did search for a similar question before posting this. I would like to know how to make Javascript code repeatedly execute while the page is open, rather than the code running once and being done or only responding to event handlers. I pretty much want the Javascript equivilant of:

$(document).ready(function() {

});

But I do not want to use Jquery because it is less efficient. I want to check an === condition every single frame.

Share Improve this question asked Oct 9, 2014 at 2:38 turkey3turkey3 352 silver badges8 bronze badges 4
  • look up setInterval() – jollarvia Commented Oct 9, 2014 at 2:39
  • Thanks! Can you give me a quick code example of how to actually incorporate it to my desired results? – turkey3 Commented Oct 9, 2014 at 2:41
  • 1 rather than the code running once and being done or only responding to event handlers This is what you should (must) do. If your code is running non-stop, then the page would freeze. If you use a setInterval loop, then it is not even an efficient approach to any problem. I don't know what you are doing here, but every time you try to execute the same code repeatedly, it will not be efficient (that says jQuery is often more efficient then the code you write.) – Derek 朕會功夫 Commented Oct 9, 2014 at 2:56
  • Can you run the === on frame load or a few ms after load using setTimeout(function(){}, 20);? Is there a frame load hook? – K'shin Gendron Commented Oct 9, 2014 at 4:11
Add a ment  | 

3 Answers 3

Reset to default 3

Use SetInterval .. inside a window.onload function

window.onload = function() {            
     function test() {
         alert("test");
     }
     setInterval(test, time_miliseconds);
 }
setInterval(function(){
     var blah = whatever;
     if (done){
         clearInterval();
     }
},time_in_milliseconds);

loop keeps looping per your milliseconds argument. If you want a loop thinner than a millisecond or as hard as the puter can do it then just a regular while(true){} will suffice.

requestAnimationFrame(callback)

docs

本文标签: Check Javascript Condition Every FrameStack Overflow