views:

30

answers:

2

hi im not sure where to start on this as events and their handlers is my weakest point in programming. ...

setInterval(function(){
   //code goes here
},1000);

as we know this runs in an infinite/loop

my question is if the code inside the function takes longer than one second (e.g: 5 seconds) to finish does the interval on the 2nd,3rd and nth second this causes the script to run slow.

as well as the above: when you click on an element with onclick function it does not run until it is it's scope turn...

is there a way to allow the 1st interval finish then run it again if the scope is 0...

in other words to put a users actions as a priority .

i don't know if im making alot of sense but please comment below and i will try and explain more.

A: 

setInterval will run a new iteration of the loop at the defined interval regardless of how long the code inside takes. It's a great way to bog down/lock up the browser.

You could handle this in a number of ways, depending on what events you're looking for and how you want it to flow.

If you're working mostly in jQuery, one option would be to use jQuery's queue() method to have your function call itself recursively once the rest of your code has completed. In simple cases that may work.

If your code is really more user-dependent, than just use one of jQuery's many event handlers like click() or bind() to trigger the code when the user takes that action.

Without more information it's hard to give you a specific solution.

Gabriel Hurley
basically the setinterval should run in a queue order as a processor would. it will check if new matched selectors have been created dynamically/ajax then assign elements with the appropriate settings. such as default attributes click events etc... so instead of writing onclick="blabla" on every single element which will have the same onclick="blabla" i could use $('.elm').click(function(){//blabla})
Val
A: 

http://www.w3schools.com/js/js_timing.asp

scroll@ Infinite Loop explains how to cancel it out or wait for it to finish before it runs again...

Val