Skip to content

Instantly share code, notes, and snippets.

@jbt
Created April 19, 2012 07:48
Show Gist options
  • Save jbt/2419469 to your computer and use it in GitHub Desktop.
Save jbt/2419469 to your computer and use it in GitHub Desktop.
jQuery jsonp auto-generated callbacks memory leak.
<?php
// Only run this locally, unless you want to bombard your server with hundreds of requests a second.
if($_GET['a']) die( $_GET['callback'] . '(false);');
?>
<!DOCTYPE html>
<html>
<head>
<title>JSONP Memory leak test</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-git.js'></script>
</head>
<body>
<div id="a"></div>
<button onclick="start();">Start</button>
<button onclick="stop();">Stop</button>
<script type="text/javascript">
var i = 0, interval, pingTo = window.location + '?a=1&callback=?';
function cb(){
document.getElementById('a').innerHTML = i++;
}
function ping(){
var j = 10;
while(j--)
$.ajax(pingTo, {
crossDomain: true,
dataType: 'jsonp',
success: cb
});
}
function start(){
interval = setInterval(ping, 10);
}
function stop(){
clearInterval(interval);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment