I can't wrap my head around javascript closures. I want 4 random numbers, but only get the the last one replicated 4 times.
Javascript
$(function() {
function setNewNumber(element) {
return function (newNumber) {
element.text(newNumber);
}
}
$('.number').each(function() {
$.get('http://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new',
setNewNumber($(this))
);
});
});
HTML
Any hints?
Answer
The get
request is being cached.
(That is to say your understanding of closures is correct and the code is working correctly).
From comments, because this is relevant:
You can request multiple numbers from random.org at the same timer per their API. Instead of using four requests, use
num=' + $(".number").length
and then do a little parsing
No comments:
Post a Comment