Linked Questions
2,912 questions linked to/from JavaScript closure inside loops – simple practical example
328
votes
10
answers
355k
views
setTimeout in for-loop does not print consecutive values [duplicate]
I have this script:
for (var i = 1; i <= 2; i++) {
setTimeout(function() { alert(i) }, 100);
}
But 3 is alerted both times, instead of 1 then 2.
Is there a way to pass i, without writing the ...
197
votes
6
answers
252k
views
Asynchronous Process inside a javascript for loop [duplicate]
I am running an event loop of the following form:
var i;
var j = 10;
for (i = 0; i < j; i++) {
asynchronousProcess(callbackFunction() {
alert(i);
});
}
I am trying to display a ...
231
votes
5
answers
75k
views
Javascript infamous Loop issue? [duplicate]
I've got the following code snippet.
function addLinks () {
for (var i=0, link; i<5; i++) {
link = document.createElement("a");
link.innerHTML = "Link " + i;
...
120
votes
6
answers
48k
views
How do I pass the value (not the reference) of a JS variable to a function? [duplicate]
Here is a simplified version of something I'm trying to run:
for (var i = 0; i < results.length; i++) {
marker = results[i];
google.maps.event.addListener(marker, 'click', function() {
...
115
votes
5
answers
119k
views
addEventListener using for loop and passing values [duplicate]
I'm trying to add event listener to multiple objects using a for loop, but end up with all listeners targeting the same object --> the last one.
If I add the listeners manually by defining boxa and ...
60
votes
5
answers
37k
views
Please explain the use of JavaScript closures in loops [duplicate]
I have read a number of explanations about closures and closures inside loops. I have a hard time understanding the concept. I have this code: Is there a way to reduce the code as much as possible so ...
61
votes
7
answers
19k
views
Doesn't JavaScript support closures with local variables? [duplicate]
I am very puzzled about this code:
var closures = [];
function create() {
for (var i = 0; i < 5; i++) {
closures[i] = function() {
alert("i = " + i);
};
}
}
function ...
23
votes
5
answers
50k
views
adding 'click' event listeners in loop [duplicate]
Refactoring standard onClick within html tag to listeners ,faced problem with my code:
var td;
for (var t=1;t<8;t++){
td = document.getElementById('td'+t);
if (typeof window....
80
votes
2
answers
55k
views
When using callbacks inside a loop in javascript, is there any way to save a variable that's updated in the loop for use in the callback? [duplicate]
Let's say I have something as follows:
for(var i = 0; i < length; i++){
var variable = variables[i];
otherVariable.doSomething(variable, function(err){ //callback for when doSomething ends
...
48
votes
3
answers
50k
views
Node.JS: How to pass variables to asynchronous callbacks? [duplicate]
I'm sure my problem is based on a lack of understanding of asynch programming in node.js but here goes.
For example: I have a list of links I want to crawl. When each asynch request returns I want to ...
23
votes
6
answers
24k
views
Pass extra parameter to jQuery getJSON() success callback function [duplicate]
I've never had to use callback functions before, so I may have made a completely stupid mistake. I think I somewhat understand the problem here, but not how to solve it.
My code (a bit simplified) is:...
36
votes
5
answers
13k
views
Access outside variable in loop from Javascript closure [duplicate]
See:
for (var i in this.items) {
var item = this.items[i];
$("#showcasenav").append("<li id=\"showcasebutton_"+item.id+"\"><img src=\"/images/showcase/icon-"+item.id+".png\" /><...
22
votes
2
answers
75k
views
jQuery AJAX calls in for loop [duplicate]
I'm new to using AJAX, and I'm writing a userscript that will process a bunch of links on a page and make AJAX calls for each one.
for (var i = 0; i < linkList.length; i++)
{
$.ajax({
...
Edge's user avatar
- 2,550
65
votes
3
answers
2k
views
What is wrong with my javascript scope? [duplicate]
The following alerts 2 every time.
function timer() {
for (var i = 0; i < 3; ++i) {
var j = i;
setTimeout(function () {
alert(j);
}, 1000);
}
}
timer();...
19
votes
7
answers
43k
views
Walking a directory with Node.js [duplicate]
I've got a problem with this code in node.js. I want to recursively walk through a directory tree and apply the callback action to every file in the tree. This is my code at the moment:
var fs = ...