Jump to content

function executing


Matej

Recommended Posts

yo, this code

var i = 0;function swapImage() { var kk=document.getElementById("branka");kk.src=path[i];var mm=document.getElementById("vole");mm.setAttribute("href",change[i]); if(i < path.length - 1&& i<change.length-1){ i++;} else {i = 0;} setTimeout("swapImage()",3000); } window.onload=swapImage;

is used for bad looking slide-show , but my question is , why is this function executed every 3s? it should be executed once

(it check if i<array.length and if its true i goes up(i++))no? there isnt setInterval , so why is this function executed every 3s? there is only delay wth seTimeout , thanks for answer

Link to comment
Share on other sites

Because it is calling itself no matter what the if else condition is, these just check the i value against the length, if i less than array length (-1) increment it, else reset it to zero, and it starts again, in both outcomes the settimout will still be called to run swapImage().

setInterval in this will be called on every loop, and every call will add another setinterval causing browser to slow and probably crash as each 3000 call is trying to be processed by the browser.

Link to comment
Share on other sites

well , think i still dont get why it is invoking itself in intervals (setInterval is NOT there and setTimeout is just delaying invoke of function) i would understand it , if there was setInterval... but setTimeout which just delay invoke of the function..

Link to comment
Share on other sites

and it starts again, in both outcomes the settimout will still be called to run swapImage().

setInterval in this will be called on every loop, and every call will add another setinterval

basicly whenever i invoke function with setTimeout within that function that i want to invoke , the setTimeOut works as setTimeout+setInterval ?

Edited by Matej
Link to comment
Share on other sites

When the function calls setTimeout inside the function, then it behaves the same as if you called setInterval outside of the function. These will do the same thing:

function func() {  alert('func');  setTimeout(func, 1000);}func();
function func() {  alert('func');}setInterval(func, 1000);
Link to comment
Share on other sites

I saw:

if(i < path.length - 1&& i<change.length-1){ i++;} else {i = 0;}

I thought:

i = ++i %path.length %change.length;

so if path has 4 elements (0...3) and change has 5 (0...4), i is first incremented by one (to 1) and then is taken by the modulo of path's length (4).

1%4 is still 1.

then its taken by the modulo of change's length (5)

1%5 is still 1.

this continues till i reaches 4...

4%4 is 0 and then 0%5 is still 0.

 

though this will mean that the change will have an item that's never accessed.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...