Linked Questions

328 votes
10 answers
355k views

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 ...
Ilyssis's user avatar
  • 4,949
197 votes
6 answers
252k views

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

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; ...
Zhu Tao's user avatar
  • 7,625
120 votes
6 answers
48k views

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

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

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

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 ...
qollin's user avatar
  • 1,281
23 votes
5 answers
50k views

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

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 ...
thisissami's user avatar
  • 16.6k
48 votes
3 answers
50k views

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

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:...
Chris's user avatar
  • 233
36 votes
5 answers
13k views

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

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

The following alerts 2 every time. function timer() { for (var i = 0; i < 3; ++i) { var j = i; setTimeout(function () { alert(j); }, 1000); } } timer();...
Naftali's user avatar
  • 147k
19 votes
7 answers
43k views

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 = ...
pvorb's user avatar
  • 7,329

15 30 50 per page
1
2 3 4 5
...
195