Skip to content

Instantly share code, notes, and snippets.

@Bajena
Last active Sep 23, 2017
Embed
What would you like to do?
Simple jQuery memory leak
const MAX_NUMBER = 10000000;
var numbers = {};
var interval = null;
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function addRandomNumbers() {
for (var i = 0; i < 1000; i++) {
var number = getRandomInt(0, MAX_NUMBER);
var newDiv = $('<div>' + number + '</div>');
numbers[number] = newDiv;
$('#numbers').append(newDiv);
}
checkMaxPresent();
}
function checkMaxPresent() {
if (numbers[MAX_NUMBER]) {
numbers[MAX_NUMBER].css('background-color', 'red');
stop();
alert('SUCCESS');
return;
}
$('#numbers').html('');
}
function stop() {
if (interval) {
clearInterval(interval);
interval = null;
}
}
$('#click-me').click(function() {
interval = setInterval(addRandomNumbers, 10);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment