Matej Posted July 10, 2014 Share Posted July 10, 2014 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 More sharing options...
dsonesuk Posted July 10, 2014 Share Posted July 10, 2014 (edited) ??? it increments causing it to loop through arrays using index ref of i , until it reaches last array item then resets i to 0, and then it continues the loop by running settimout. Edited July 10, 2014 by dsonesuk Link to comment Share on other sites More sharing options...
Matej Posted July 10, 2014 Author Share Posted July 10, 2014 (edited) so when there is increment in if statement it behaves like (for)loop? Edited July 10, 2014 by Matej Link to comment Share on other sites More sharing options...
Matej Posted July 10, 2014 Author Share Posted July 10, 2014 http://jsfiddle.net/N2aP2/ i tried it here , so it seems it really works like (for)loop , but why is setTimeout behaving like setInterval? Link to comment Share on other sites More sharing options...
dsonesuk Posted July 10, 2014 Share Posted July 10, 2014 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 More sharing options...
Matej Posted July 10, 2014 Author Share Posted July 10, 2014 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 More sharing options...
Matej Posted July 10, 2014 Author Share Posted July 10, 2014 (edited) 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 July 10, 2014 by Matej Link to comment Share on other sites More sharing options...
justsomeguy Posted July 10, 2014 Share Posted July 10, 2014 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 More sharing options...
Matej Posted July 10, 2014 Author Share Posted July 10, 2014 thats it! thanks you very much Link to comment Share on other sites More sharing options...
Hadien Posted July 10, 2014 Share Posted July 10, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now