【问题标题】:Cancel new request instead of currently loading request when refreshing an image with Angular's $timeout使用 Angular 的 $timeout 刷新图像时取消新请求而不是当前加载请求
【发布时间】:2014-08-02 18:43:21
【问题描述】:

我正在使用 Angular 的超时以用户指定的时间间隔刷新图像的内容。当该间隔太短而无法加载图像时(例如 1 秒),每个后续请求都会取消前一个请求,从而导致图像永远不会刷新。

我希望完成当前请求并取消后续请求,直到请求冲突时该请求完成,而不是这种行为。我该怎么做?

处理刷新的代码:

$scope.setImagePath = function() {
  $scope.imagePath = $scope.camera.endpoint + '?decache=' + Math.random();
};
$scope.setImagePath();

$scope.refreshOnInterval = function() {
  $timeout(function() {
    $scope.setImagePath();
    $scope.refreshOnInterval();
  }, $scope.refresh);
};
$scope.refreshOnInterval();

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    看看this question的回答。那里展示的事件将为实施提供基础。

    我的建议是使用imageonload 指令作为开始。而不是alert-ing 的东西,用图像的加载状态更新一个变量。然后,在您的 $timeout 处理程序中,检查此状态变量,如果状态仍在加载,请不要调用 setImagePath()。您可能还需要更新 setImagePath() 中的状态变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-25
      • 2020-03-30
      • 2018-11-07
      • 2016-05-24
      • 2018-07-26
      • 2018-12-12
      • 1970-01-01
      • 2016-03-26
      相关资源
      最近更新 更多