Skip to content

Instantly share code, notes, and snippets.

@jodosha
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jodosha/9269521 to your computer and use it in GitHub Desktop.
Save jodosha/9269521 to your computer and use it in GitHub Desktop.
Limit endless page loading, by wrapping long running functions invocation in a setTimeout. Run with `bash server.sh`.
<!DOCTYPE html>
<html>
<head>
<script>window.PROFILER||(PROFILER={});PROFILER.data={start:(new Date).getTime()}</script>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
<title>JS Timeout</title>
</head>
<body>
<div id="loaded"></div>
<div id="long-running"></div>
<script>
function longRunning() {
var milliseconds = 10000;
var date = new Date();
var current = null;
do { current = new Date(); }
while(current - date < milliseconds);
$('#long-running').text('finished');
}
</script>
<script>
if(PROFILER && PROFILER.data){
PROFILER.data['end']=(new Date).getTime();
$(document).ready(function(){PROFILER.data['ready']=(new Date).getTime()});
$(window).load(function(){
PROFILER.data['load']=(new Date).getTime();
$('#loaded').text('loaded in ' + (PROFILER.data['load'] - PROFILER.data['start']).toString() + ' ms');
window.setTimeout(longRunning, 100);
});
}
</script>
</body>
</html>
#!/bin/bash
ruby -run -e httpd . -p 5000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment