I want to get value inside of my function $interval and use it as interval time.
I have index.html:
<iframe src="{{MediaFile}}" frameborder="0" allowfullscreen autoplay></iframe>
And MediaFile table:
+------+-------------+--------------+
| ID | FileName | Interval | //Interval in secs
+------+-------------+--------------+
| 1 | image.jpg | 30 |
+------+-------------+--------------+
| 2 | image1.jpg | 10 |
+------+-------------+--------------+
| 3 | video.mp4 | 50 |
+------+-------------+--------------+
And index.js:
// loadMediaFile, select the list of MediaFile in my table
loadMediaFile.MediaFiles('loadMediaFile').then(function (d) {
$scope.IntervalTime = 2000;
$scope.Count = -1;
$interval(function () {
$scope.Count++
$scope.MediaFile = '/MediaFiles/Content/' + d[$scope.Count].FileName;
$scope.IntervalTime = d[$scope.Count].Interval * 1000;
// This is to load back to index 0 of my MediaFile when all is loaded
if (d.length == ($scope.Count + 1)) {
$scope.Count = -1;
}
}, $scope.IntervalTime);
console.log($scope.IntervalTime);
})
Output should be:
Displays Image.jpg after 2secs
Displays Image1.jpg after 30
Displays Video.mp4 after 10
Displays Image.jpg after 50secs
The problem is the interval stays at 2 secs, it doesn't update the interval value since the correct value is inside of my $interval. That's why I want to return the correct value to $scope.IntervalTime like using the return.
$intervalis a Promise - an asynchronous callback. You can't extract any values from it "outside", but you can keep your entire logic inside it. For example updating some counter, which will seemingly change your model in HTML code (like swap images in carousel because the counter went up)