27
votes
12answers
2k views

What is the best way to do loops in JavaScript

I have stumbled into several methods of looping in JavaScript, what I like the most is: for(var i = 0; i < a.length; i++){ var element = a[i]; } But as tested here ...
25
votes
2answers
6k views

Looping through array and removing items, without breaking for loop

I have the following for loop, and when I use splice() to remove an item, I then get that 'seconds' is undefined. I could check if it's undefined, but I feel there's probably a more elegant way to do ...
20
votes
5answers
4k views

Access outside variable in loop from Javascript closure

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\" ...
12
votes
5answers
15k views

jQuery loop through data() object

Is it possible to loop through a data() object? Suppose this is my code: $('#mydiv').data('bar','lorem'); $('#mydiv').data('foo','ipsum'); $('#mydiv').data('cam','dolores'); How do I loop ...
11
votes
8answers
8k views

How to stop intense Javascript loop from freezing the browser

I'm using Javascript to parse an XML file with about 3,500 elements. I'm using a jQuery "each" function, but I could use any form of loop. The problem is that the browser freezes for a few seconds ...
9
votes
5answers
608 views

Closures in a for loop

Closures in a loop are causing me problems. I think I have to make another function that returns a function to solve the problem, but I can't get it to work with my jQuery code. Here is the basic ...
9
votes
3answers
8k views

loop through json array jquery

I'm trying to loop through this to get the 'name' values. This is what I currently have, but it doesn't seem to be working, tried a few others from what was posted here but nothing seemed to work. ...
9
votes
5answers
10k views

how to dynamically exit a jquery $.each()?

i have a list of images which i am getting through ajax and then using jquery $.each() i loop through the images and display a image one after the other after an interval of one second. I want the ...
9
votes
1answer
5k views

Looping through Markers with Google Maps API v3 Problem

I'm not sure why this isn't working. I don't have any errors, but what happens is, no matter what marker I click on, it always clicks the last marker. Im not sure why though because the_marker is set ...
9
votes
4answers
8k views

How to write a loop in jQuery which waits for each function to complete before continuing the loop

Please forgive me if this is an obvious one. I have an unknown amount of elements on a page, which I need to loop through one at a time and do stuff to. However, I need the loop to pause until the ...
8
votes
6answers
1k views

Strange things in JavaScript “for”

I'm using jQuery and I have a strange thing that I don't understand. I have some code: for (i = 1; i <= some_number; i++) { $("#some_button" + i).click(function() { alert(i); }); } ...
8
votes
9answers
12k views

How to make a jquery infinite animation?

I'm trying to implement a jQuery function with an infinite loop to animate the body background with 3 colours. I cannot think of a nice and clean solution. Something like this? ...
1
vote
3answers
31 views

Is there a way to track an infinite loop with console.log or something alike?

i was wondering if there is any way of tracking an infinite loop in javascript/jquery ? I am working on a jsTree plugin clone sort of.. (why isn't important) and I've create a infinite loop somewhere ...
0
votes
1answer
20 views

Skrollr.js: Looping action that will happen all the time while user is scrolling window

Surprisingly, I can't find an answer to this question in Stack Overflow. Seems no one has a need for what I'm specifically after. However, Skrollr has no real documentation I can find, so there's ...
0
votes
4answers
57 views

Functions in a for Loop

I have some code I am trying to get to iterate over some images in a slider effect. However when I try to call the function from within my for loop it doesn't work. But if I have the call to ...

1 2 3 4 5 66
15 30 50 per page