Created
April 9, 2024 10:52
-
-
Save hritik5102/5a6cca2009c8d71a4f2c12744b344f6c to your computer and use it in GitHub Desktop.
Defer attribute in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Async vs Defer</title> | |
<script> | |
console.log('Start HTML Parsing'); | |
</script> | |
</head> | |
<body> | |
<p>Hello, World</p> | |
<script src="index.js" defer></script> | |
<div class="content"> | |
<span>Span element</span> | |
</div> | |
<script> | |
console.log('End HTML Parsing'); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DOM Event fire after script is executed. | |
document.addEventListener('DOMContentLoaded', () => | |
alert('DOM ready after defer!') | |
); | |
let sum = 0; | |
for (let i = 0; i < 10000000; i++) { | |
sum += i; | |
} | |
console.log('Script is Executed, Sum is : ', sum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment