1

loop that executes a function. However, the following works - the counter counts while the images on the page are loaded.

var progress = function()
 {
   for (var i = 0; i < slide.length; i++)
    slide[i].onload = function(){
         actualprogress +=1,
         loading.innerHTML = actualprogress
    };
 }

While the following does not work. When I open the Page the counter says "[n]" (number of slides, e.g. "12") from the beginning.

var progress = function()
 {
   var action = function(){
     actualprogress +=1;
     loading.innerHTML = actualprogress
   }
   for (var i = 0; i < slide.length; i++)
     slide[i].onload = action();
 }

I would like to call a function from the for-loop because I will need to do other and more things within the function. Why doesn't this work?

2
  • Also think about using indentation Commented Nov 10, 2012 at 18:05
  • 1
    I did now, sorry about that and thank you for telling me! Commented Nov 10, 2012 at 18:15

2 Answers 2

2

In the first code you are assigning the function pointer to the onload property.

In the second code you are assigning the function returned value to the onload property which is null.

There is a huge difference between those. The parantheses are extra in the second code, after action.

onload = action() => onload = action

Cheers

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

Comments

2

On the last line of the second snippet, you're calling the function, instead of just assigning it to slide[i].onload.

Simply change slide[i].onload = action(); to slide[i].onload = action;.

Comments

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.