JavaScript Play a sequence of images with JS, using $timeout to schedule frames jquery play sequence images,matlab play sequence of images,javascript

I'm using AngularJS to prefetch images in cache client and then I want to animate those prefetched images.

My code for the prefetching:

$scope.prefetch=function(limit) {
  for (var i=0; i<limit; i++) {
    var date = new Date($scope.dt);
    if ($scope.fileFlag == false) {
      if ($scope.viewmodel.timeResolution == 'yearly')
        date = new Date(date.setFullYear(date.getFullYear() + i));
      else if ($scope.viewmodel.timeResolution == 'monthly')
        date = new Date(date.setMonth(date.getMonth() + i));
      else if ($scope.viewmodel.timeResolution == 'daily') {
        date = new Date(date.setDate(date.getDate() + i));
      }
    } else {
      date = $scope.files[$scope.files.indexOf($scope.idSelectedVote) + i];
    }

    console.log( $http.get(site_url + "mwf/" + $scope.viewmodel.dataSet + "/" + $scope.viewmodel.varName + "/" + $scope.viewmodel.region + "/" + date + "/map/?vMin=" + $scope.VMin + "&vMax=" + $scope.VMax + "&type=" + $scope.viewmodel.type + "&cmap=" + $scope.viewmodel.colorMap, {'cache': true}));
  }
};

then i do something like this to play those images

$scope.play=function(limit) {
  for (var i=0; i<limit; i++) {
    $scope.map.src= site_url + "mwf/" + $scope.viewmodel.dataSet + "/" + $scope.viewmodel.varName + "/" +  $scope.viewmodel.region + "/" +  parseInt(date)+i + "/map/?vMin=" + $scope.VMin + "&vMax=" + $scope.VMax + "&type=" + $scope.viewmodel.type + "&cmap=" + $scope.viewmodel.colorMap;
    $scope.sleepFor(500);
  }                
};

$scope.sleepFor = function( sleepDuration ) {
  var now = new Date().getTime();
  while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}

My problem is when I call play(4) it displays only the first and the last images and not an animation. Any idea on how can I improve this code or a different approach so I can do this?

Answer:1

You have to use intervals otherwise your code will block the execution of other code

Using Angular's built in $interval service is the solution:

var playInterval;
$scope.play = function(limit) {
    var interval = 1000 / 20; //20 frames per second
    var i = 0;
    $interval.cancel(playInterval); //stop previous animations if any
    if(i < limit) {
        $scope.map.src = getSrc(i++);                   
        var cache = $interval(function() {
            if(i >= limit) {                                        
                return $interval.cancel(playInterval); //or you can replace with `i = 0;` to loop the animation
            }
            $scope.map.src = getSrc(i++);
        }, interval);
    }
};

function getSrc(i) {
    return site_url + "mwf/" + $scope.viewmodel.dataSet + "/" + $scope.viewmodel.varName + "/" +  $scope.viewmodel.region + "/" +  parseInt(date)+i + "/map/?vMin=" + $scope.VMin + "&vMax=" + $scope.VMax + "&type=" + $scope.viewmodel.type + "&cmap=" + $scope.viewmodel.colorMap;
}
Answer:2

I have a div table and would like to get the id value of the class live-mu-table-tdq that is to the left of where I click. I thought that the following code would work but it didn't. $(this).closest(...

I have a div table and would like to get the id value of the class live-mu-table-tdq that is to the left of where I click. I thought that the following code would work but it didn't. $(this).closest(...

Is it possible to create dynamic templates/pages with TVML without relying on Apple's standard templates? Say I'd like to create a template that has 2 images next to each other and on top of these ...

Is it possible to create dynamic templates/pages with TVML without relying on Apple's standard templates? Say I'd like to create a template that has 2 images next to each other and on top of these ...

My visual editor in WordPress doesn't work and I figured out that was because my computer doesn't load the full tinymce folder. It only loaded "mce-view.js". In another computer, using Chrome, around ...

My visual editor in WordPress doesn't work and I figured out that was because my computer doesn't load the full tinymce folder. It only loaded "mce-view.js". In another computer, using Chrome, around ...

  1. chrome fails to load pages
  2. chrome fails to load
  3. chrome failed to load pdf
  4. chrome fails to load any page
  5. google chrome fails to load pages
  6. google chrome fails to load
  7. google chrome failed to load web pages
  8. chrome randomly fails to load pages

I am unable to append the page just seems to refresh where as i want to view it without refreshing the page index.php <div class="view_comment"> <b>username :</b> <?php ...

I am unable to append the page just seems to refresh where as i want to view it without refreshing the page index.php <div class="view_comment"> <b>username :</b> <?php ...