JavaScript $timeout inside will loop creates infinite loop

I cannot seem to get the timeout and the loop to work together. If I remove the loop it works or if I remove the timeout it will work. Together I will get an infinit loop and it doesnt seem like the timeout function ever runs if its inside of the while loop.

Any help would be appreciated!

function doTask (list) {
    if (list) {
        while (list.length > 0) {  
            (function(list){
                $timeout(function() {   
                   list.splice(0, 1); 
                   console.log("timeout...") 
                }, 5000); 
            })(list); 
        }
    }
}
Answer:1

You should use recursion to loop. Also, you should check list.length as [] is a true value.

function doTask(list) {
  if (list.length) {
    setTimeout(function() {
      list.splice(0, 1);
      console.log("timeout...")
      doTask(list);
    }, 500);
  }
}
var list = [1,2,3,4,5]
doTask(list);
Answer:2

I have written a custom callback function for Javascript's find function but that is always yielding undefined var objectsArray = [ { 'a': 1, 'b': 2, 'c': 3 }, { 'a': 41, 'b': 5, 'c': 7 }, ...

I have written a custom callback function for Javascript's find function but that is always yielding undefined var objectsArray = [ { 'a': 1, 'b': 2, 'c': 3 }, { 'a': 41, 'b': 5, 'c': 7 }, ...

I have this [sample][1], what I want is to have this result[![enter image description here][2]][2] Note: a.) 1 and 2 will be connected while 3 will be produced in the third mousedown click. b.) 1,...

I have this [sample][1], what I want is to have this result[![enter image description here][2]][2] Note: a.) 1 and 2 will be connected while 3 will be produced in the third mousedown click. b.) 1,...

  1. make parallel lines in illustrator
  2. make parallel lines photoshop
  3. make parallel lines in autocad
  4. make parallel lines
  5. make parallel lines adobe illustrator
  6. make parallel lines in sketchup
  7. make lines parallel solidworks
  8. make lines parallel powerpoint
  9. inkscape make parallel lines
  10. make parallel curved lines
  11. how to make parallel lines with a compass
  12. how to make parallel lines using compass
  13. how to make parallel lines in construction
  14. how to make parallel lines with set squares
  15. how to make parallel lines on geogebra
  16. how to make parallel lines geometry
  17. how to make parallel lines on geometer's sketchpad
  18. make two lines parallel illustrator
  19. make two lines parallel in autocad
  20. how to make parallel lines on keyboard

I have this clock object: var Clock = { start: function () { $('#btnScrollPause').show(); $('#btnScrollResume').hide(); advance(); this.interval = setInterval(function () ...

I have this clock object: var Clock = { start: function () { $('#btnScrollPause').show(); $('#btnScrollResume').hide(); advance(); this.interval = setInterval(function () ...

  1. callback in this.setstate
  2. javascript callback with this
  3. typescript callback with this
  4. js callback with this
  5. callback function with this
  6. c++ callback with this
  7. jquery callback with this
  8. javascript pass callback with this
  9. javascript callback function with this
  10. typescript callback function with this

I'am studying React JS library, and i wish create a "loop" of component, returning array values from json file. Here my json file: { "data": { "children": [{ "data": { "score": 4 ...

I'am studying React JS library, and i wish create a "loop" of component, returning array values from json file. Here my json file: { "data": { "children": [{ "data": { "score": 4 ...

  1. loop component in react
  2. loop component in talend
  3. create react component with loop