1

I am using AngulaJs and Ionic.

I am trying to call a function every 3 seconds for 10 times only (this is why I am not using $interval). Thing is, the code below calls the function only once (while the debugging code to the console is called 10 times).

for (i = 0; i < 10; i++)
 {
   $timeout(function () {
     $scope.play(); // this is the called function
     console.log("progress bar: " + i);
   }, 3000);
 }

Any help will be appreciated,

Thanks in advance,

Pagugim

4
  • There's nothing wrong with the code you've posted. You must have a blocking action in $scope.play() since my plunker works perfectly: plnkr.co/edit/izZvfrLrGxmLD5n0AnYS?p=preview. NOTE: what you are doing will not have the outcome that you think it will. It will quickly create 10 iterations, wait three seconds per the timeout and resolve them all simultaneously, since $timeout returns a promise and will not block. This should be a recursive operation, as Mike noted below. Commented Jan 19, 2016 at 21:05
  • no it's not right....will add numerous timers in each loop that will all fire at the same time. Loop will complete in milliseconds Commented Jan 19, 2016 at 21:11
  • @charlietfl while that's true, the OP specifically said "the code below calls the function only once", which is an untrue statement. It will be called 10 times, all at once. Commented Jan 19, 2016 at 21:15
  • Hi, thank you very much for the fast reply (-: Commented Jan 19, 2016 at 21:57

3 Answers 3

4

For loop will start 10 timeouts all together. But seems like you want them to execute one after another. For this case you can use recursion.

var count = 0;
replay();

function replay() {

    $timeout(function () {
     if(count < 10) {
         $scope.play();
         count++;
         replay(); // call this function again
         console.log("progress bar: " + $scope.progressVal);
     }
    }, 3000);
}
Sign up to request clarification or add additional context in comments.

Comments

4

I am trying to call a function every 3 seconds for 10 times only

You can use the optional count argument of $interval.

var i=0;
function reportProgress() {
    $scope.play(); // this is the called function
    console.log("progress bar: " + i);
    i++;
};
$interval(reportProgress, 3000, 10);

From the Docs:

$interval

Usage $interval(fn, delay, [count], [invokeApply], [Pass]);

count (optional) Number of times to repeat. If not set, or 0, will repeat indefinitely. (default: 0)

-- AngularJS $interval API Reference

Comments

1

Actually, the $scope.play() is called 10 times, but at almost the same time. To call the function every 3 seconds, you can use a closure to keep the value i and set timeout as 3000*i. I think this is what you want:

for (i = 0; i < 10; i++) {
    (function(i) {
        $timeout(function() {
            $scope.play();
            console.log("progress bar: " + i)
        }, 3000 * i);
    })(i);
}

Jsfiddle: https://jsfiddle.net/ealonwang/ens9wu0g/15/. Watch the value and console log change every 3 seconds.

1 Comment

Hi, thank you very much for the fast reply (-: I used your example and it worked perfectly for me. Thanks again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.