Last active
September 23, 2017 13:49
-
-
Save Bajena/87b36ff00cfd1e083666fba9b1dee7f3 to your computer and use it in GitHub Desktop.
Simple jQuery memory leak
This file contains 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
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