1

Hi i have a problem with recursion.

i followed this example from wc3 http://www.w3schools.com/jsref/met_win_settimeout.asp But mine seems to not work at all.

function rotateImages(start)
  {
  var a = new Array("image1.jpg","image2.jpg","image3.jpg", "image4.jpg");
  var c = new Array("url1", "url2", "url3", "url4");
  var b = document.getElementById('rotating1');
  var d = document.getElementById('imageurl');
  if(start>=a.length)
      start=0;
  b.src = a[start];
  d.href = c[start];
  window.setTimeout("rotateImages(" + (start+1) + ")",3000);
  }

  rotateImages(0);

Firebug throws the error :

rotateImages is not defined
[Break On This Error] window.setTimeout('rotateImages('+(start+1)+')',3000);

However if i change the timeOut to :

window.setTimeout(rotateImages(start+1),3000);

It recursives but somehow the delay doesn't work and gives me too much recursion(7000 in a sec)

2
  • W3Schools is not the W3C. They are a low quality, advert covered, third party tutorial site with good SEO. Commented Jun 8, 2011 at 18:55
  • Any good sites you recommend? Commented Jun 9, 2011 at 0:59

3 Answers 3

3

There are many reasons why eval should be avoided, that it breaks scope is one of them. Passing a string to setTimeout causes it to be evaled when the timer runs out.

You should pass a function instead.

window.setTimeout(rotateImages(start+1),3000);

This calls rotateImages immediately, then passes its return value to setTimeout. This doesn't help since rotateImages doesn't return a function.

You probably want:

window.setTimeout(rotateImages,3000,[start+1]);

Or create an anonymous function that wraps a closure around start and pass that instead:

window.setTimeout(function () { rotateImages(start + 1); },3000);

The latter option has better support among browsers.

Sign up to request clarification or add additional context in comments.

2 Comments

The 2nd method doesn't work with internet explorer so the last is preferred (or use a defined function). blog.skinkers.com/2011/05/31/… has a nice writeup of how to deal with it as well (using a function that returns a function)
All you need to know about eval is that you shouldn't use it. It screws up scope. It is slow. It is hard to debug. It risks introducing security problems. Use the last example in my answer.
1

Be wary of code from W3Schools.

The other answers give a solution. I'll just add that you're recreating the Arrays and repeating the DOM selection every time the rotateImages function is called. This is unnecessary.

You can change your code like this:

(function() {
    var a = ["image1.jpg","image2.jpg","image3.jpg", "image4.jpg"];
    var c = ["url1", "url2", "url3", "url4"];
    var b = document.getElementById('rotating1');
    var d = document.getElementById('imageurl');

    function rotateImages(start) {
      b.src = a[start];
      d.href = c[start];
      window.setTimeout(function() {
          rotateImages( ++start % a.length );
      }, 3000);
    }

    rotateImages(0);
})();

3 Comments

Thanks for the answers and helping to refactor my code as well!
i love this line! ++start % a.length
@Yz Low: You're welcome. Yes, using the modulus operator is a nice way to reset to 0.
1

Try this syntax:

window.setTimeout(function() {
    rotateImages(start+1);
},3000);

setTimeout() expects a function reference as the 1st parameter. Simply putting a function call there would give the return value of te function as the parameter, this is why the delay did not work. However your first try with evaluating a string was a good approach, but it is not recommended.

2 Comments

Now i understand.. How do i know when I should put the function call?
If you need your function's return value. If you want to give your function (to call it later) you should use the function reference (without the braces) or an anonymous function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.