Skip to content

Instantly share code, notes, and snippets.

@lizlux
Last active December 12, 2018 03:27
Show Gist options
  • Save lizlux/808efdb185adb402449a48653463fc57 to your computer and use it in GitHub Desktop.
Save lizlux/808efdb185adb402449a48653463fc57 to your computer and use it in GitHub Desktop.
Testing window load with ajax call race condition
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
<h1>Test race condition</h1>
<script>
console.log('start script');
function init() {
console.log('init called');
$(window).on('load', () => {
// doesn't get called (unless super fast connection)
console.log('window loaded from inside init');
});
}
$.get('https://data.sfgov.org/resource/wwmu-gmzc.json', () => {
console.log('got the response');
init();
});
$(window).on('load', () => {
console.log('window loaded');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment