0

I have a code like this:

    balls_delivery1 = [6, 7, 8, 6, 6] // balls in a over(cricket match)
    // above 6 balls in 0th over so the counter(key) below will be till 0.6 
     //and then 1.1 to 1.7 and so on
    deliveries1 = [{
    '0.1': {    // 0.1 is 0th over 1st ball
      batsman: 'MEK Hussey',
      bowler: 'IK Pathan',
      non_striker: 'S Badrinath',
      runs: [Object]
    }
  },{}... so on many objects]
     



for (i=0; i<balls_delivery1.length; i++){
     for (j=0; j<balls_delivery1[i]; j++){
        // i is over j is ball
         console.log(`${i} over ${j+1} ball , ${i}.${j+1}`);
         console.log(Object.values(deliveries1[j])[0]);
         // I want to show the above lines every 5 sec
      }
  }

Please do help, I could not solve it from ans already on stackoverflow.

Vinita
  • 1,834
  • 2
  • 8
  • 20
  • `setInterval(() => console.log('hello'), 5000)` – derpirscher Apr 24 '21 at 15:40
  • my end goal is not just printing hello but printing it from for loops, since this code I need in a bigger application where the loops are required. – Vinita Apr 24 '21 at 15:42
  • Then describe your problem, you try to solve ... With the given information, nobody can help you ... – derpirscher Apr 24 '21 at 15:44
  • I have uploaded my exact problem now @derpirscher, can you help now? – Vinita Apr 24 '21 at 16:01
  • @BrownPaul some overs may have wide balls for that one extra ball is there so making it total of 7 balls in that over, and like so 8 also – Vinita Apr 24 '21 at 16:32

4 Answers4

3

You can try something like this.

for (i=0; i<10; i++){
  (function(index_i) {
      setTimeout(function() {
        for (j=0; j<7; j++){
          (function(index_j) {
              setTimeout(function() { console.log('hello ' + index_i + ' - ' + index_j); }, j * 5000);
          })(j);
        }
      }, i * 7 * 5000);
  })(i);
}
Ayaz
  • 2,111
  • 4
  • 13
  • 16
  • I'd say you also have to multiply by `i`. Only then each timeout has an unique distance of 5000ms to the other. Your current code lets 10 timouts execute plesiochronously, because you pass the inner loop 10 times and always generate the same distances on the timeline. –  Apr 24 '21 at 16:00
  • Yes I had missed that part. I have updated my answer. – Ayaz Apr 24 '21 at 16:05
  • this works for 'hello', but if I print i , i is printed as the last value of the i(here 10), can I not get i from 0 and j also 0 then after 5 sec i=1, j=1 like this. – Vinita Apr 24 '21 at 16:15
  • 1
    @Vinita, I have updated the answer. You can use index_i and index_j for the value of i and j. – Ayaz Apr 24 '21 at 16:30
  • yes, thanks I think it is working for me :) – Vinita Apr 24 '21 at 16:38
2

Use function inside the loop for setTimeout, like this;

(function(){setInterval(() => console.log('hello'), 5000)})()
Shubham Ambastha
  • 194
  • 3
  • 11
1

Do you mean, you want to print 70 hellos every 5 seconds? Because this is what happens when you use your loop solution. Don't do it this way!

for (i=0; i<10; i++){
    for (j=0; j<7; j++){
        console.log('hello');  // I want to print this hello every 5 seconds
    }
}

Or do you just want to print hello every 5 seconds and you tried to solve this with a loop?

Here is how to usually print hello every 5 seconds

// start the interval and memorize it
const interval = setInterval( () => {

    console.log('hello');

}, 5000);


// stop the interval somewhere else in your code
clearInterval(interval);

If you used your loop here, you would have to have an array to store all 70 intervals inside in order to have the chance to stop them if necessary. I cannot imagine that this is what you want.

1

function testTimeout(){
    function consoleHello(i){
    setTimeout(function(){console.log('Hello' + i);}, 800*i); //setTimeout for 800ms
  }
  
    for (var i=0; i<10; i++){
      consoleHello(i);
  }
}

This is simple - working code with setTimeout inside a loop. Let me update answer for your code

The es6 variant for a working snippet

for (let i=0; i<=10; i++) {
    setTimeout(() => {console.log(i);}, 1000 * i);
}