Skip to content

Instantly share code, notes, and snippets.

@cou929
Created December 25, 2013 12:59
Show Gist options
  • Save cou929/8123027 to your computer and use it in GitHub Desktop.
Save cou929/8123027 to your computer and use it in GitHub Desktop.
test for setting timing of onload event listener
<!DOCTYPE html>
<html>
<head>
<script>
// for caching
(new Image()).src = 'http://upload.wikimedia.org/wikipedia/commons/2/23/1x1.GIF?a=1';
</script>
</head>
<body>
<div>
<h2>set src attribute before attaching onload listener</h2>
<p>is onload called: <span id="result1"></span></p>
<p>complete attribute: <span id="result2"></span></p>
</div>
<div>
<h2>set src attribute after attaching onload listener</h2>
<p>is onload called: <span id="result3"></span></p>
<p>complete attribute: <span id="result4"></span></p>
</div>
<script>
setTimeout(function(){
// set src attribute before attaching onload listener
var img = new Image();
img.src = 'http://upload.wikimedia.org/wikipedia/commons/2/23/1x1.GIF?a=2';
img.onload = function() { document.getElementById('result1').innerHTML = 'called'; };
document.getElementById('result2').innerHTML = img.complete;
}, 500);
setTimeout(function(){
// set src attribute after attaching onload listener
var img = new Image();
img.onload = function() { document.getElementById('result3').innerHTML = 'called'; };
img.src = 'http://upload.wikimedia.org/wikipedia/commons/2/23/1x1.GIF?a=3';
document.getElementById('result4').innerHTML = img.complete;
}, 500);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment