1

Here is the code:

function getTime(j){
  var stopClock= new Date();
  delta[parseInt(j)]=stopClock.getMilliseconds()-start.getMilliseconds();
 }

 //REST OF THE CODE!!

for (var i = 0; i < 6; i++){
  start = new Date();
  document.write('<img src="'+URL[i]+'" width="1" height="1" alt="" onload="getTime('+i+');"/>');
 }

the problem is sometimes in some values of j (e.g. 3 or 5) it says getTime is not defined! (most of the time is fine)...

I know the it may be because of the fact that when it reaches to that onload line it may not have loaded getTime function yet, but getTime function is at the top of the code and many of the getTime has response...

2
  • Did you try debugging using a JS debugging tool like firebug ? Commented Aug 23, 2011 at 21:01
  • Yes this is how I am getting the error! (getTime is not defined) Commented Aug 23, 2011 at 21:04

2 Answers 2

2

I am not sure what you are trying to do (especially using document.write), but you could try putting the function into a proper scope such a document scope.


    // pass the start time in milliseconds as well
    document.getTime = function(startInMillis, j) {
        var stopClock= new Date();
        delta[j]=stopClock.getMilliseconds()-start.getMilliseconds();
    };

And try using document.getTime() from the next line of code. You also see that I put the start variable in getTime() as well. Given that loading time of the image is not synchronous (though your loop is), you need make sure you pass this into the function otherwise you could have gotten a wrong result.

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

3 Comments

Thanks, this solves my problem, yet now delta is not defined! but I have a var delta = []; on the top of the script!
by the way the line should be like this: delta[parseInt(j)]=stopClock.getMilliseconds()-startInMillis;
where do you instantiate the delta? What you might want to do is wrap the entire thing in onload function. For example in jquery you want to use $(document).ready() function
0

I would think the manner of trying to introduce a callback to your request is not the correct path. It sounds like you need to potentially move to another solution perhaps using some jsonP or another technology to get around the same domain policy and then you could attach to the appropriate events.

onload isnt even a proper event for an image tag. onload should only be valid on a <frameset> or <body> tag.

2 Comments

how would you suggest to measure the amount of time needed to download an image?
onload is supported for <body>, <frame>, <frameset>, <iframe>, <img>, <input type="image">, <link>, <script>, <style>.

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.