- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 requestAnimationFrame 创建一个带有滚动数字的 javascript 动画
为了看到动画滚动,我需要使用 $apply 来刷新 View 。这里的问题是当我开始动画时,摘要循环已经在运行所以我的 $apply 返回错误“$apply already in progress”。
我正在寻找一种解决方案,我使用 $timeout of 0 效果很好,但我想知道是否有其他解决方案可以避免使用在性能方面不太好的超时?
感谢您的帮助!
HTML代码:
<div ng-app="myApp">
<div ng-controller="myController as ctrl">
<animated-counter data-from='0' data-to='{{ctrl.limit}}'></animated-counter>
</div>
</div>
Javascript 代码:
(function(){
'use strict';
angular
.module('myApp', [])
.directive('animatedCounter', AnimatedCounter);
AnimatedCounter.$inject = ['$timeout'];
function AnimatedCounter($timeout) {
return {
restrict: 'E',
template: '<div>{{num}}%</div>',
link: function (scope, element, attrs) {
scope.num = parseInt(attrs.from);
var max = parseInt(attrs.to);
//Loop to increment the num
function animloop() {
if (scope.num >= max) { //Stop recursive when max reach
return;
}
requestAnimationFrame(animloop);
scope.$apply(function() {
scope.num += 1;
});
}
// $timeout(function() { //if I use $timeout it works perfectly
animloop();
// });
}
};
}
angular
.module('myApp')
.controller('myController', myController);
function myController() {
var vm = this;
vm.limit = 100;
}
})();
你可以在这里找到一个 CodePen
最佳答案
如果我像 HadiJZ 所说的那样使用 $evalAsync() ,效果会很好!
只需要更换
scope.$apply(function() {
scope.num += 1;
});
由
scope.$evalAsync(function() {
scope.num += 1;
})
这里有一篇好文章http://www.bennadel.com/blog/2605-scope-evalasync-vs-timeout-in-angularjs.htm
关于angularjs - 需要将 $timeout 和 $apply 与 requestAnimationFrame 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35957361/
我正在使用 heroku 来托管支持 iOS 应用程序的 ruby on rails 应用程序。我有一个可能会运行很长时间的请求,我需要能够在我的请求被终止之前捕获超时。我正在使用 Timeout
我在 https://stackoverflow.com/questions/517219?tab=oldest#tab-top 找到了我认为应该完美运行的东西但是,它对我不起作用。 我在 Windo
我有这段代码: begin complete_results = Timeout.timeout(4) do results = platform.search(artist,
我正在开发一个音乐训练游戏,我使用 Unity 3D 来创建它。 它会发出随机音符。 问题是,我的 android 设备只有五个“屏幕熄灭前的时间”选项,其中最长的是 10 分钟。 所以 10 分钟后
我运行以下代码来捕获任何可能挂起的 SQL 语句。在尝试对此进行测试时,我编写了一个非常优化的 sql 语句,它需要一分钟的时间才能运行。我在 activerecord execute sql 语句周
由于 Faraday 没有文档,我无法在任何地方找到它。法拉第什么是“timeout”,什么是“open timeout”? 最佳答案 如果您在 https://github.com/lostisla
我想对 Angular.js $timeout 进行单元测试,以检查是否已使用正确的持续时间/延迟值调用它。 断言看起来像这样: expect($timeout).toHaveBeenCalledWi
我正在循环一个列表并对列表中的每个成员执行一些操作。如果某个成员花费了太多时间(在本例中为 1 秒),我打算跳过它。但是,try 语句内的 block 始终处于处理状态,并且永远不会超时。我不明白为什
我有一个程序可以打印出通过或失败。我想检测卡在那里的程序并回显“超时” 我写了这样一个脚本: #!/bin/bash echo -n 'test' && timeout 5 ./mytest | gr
我有一个 Sinatra 应用程序。我正在使用 Rack::Test 对其进行测试。我想确保将查询字符串参数传递给 Timeout::timeout()。 我认为 expect_any_instanc
相同的脚本不同的错误。这可能更多地与我的网络有关,而不是我的代码。脚本如下: #!/usr/bin/env ruby -rubygems require File.join(File.dirname(
我需要测试一个从 url 加载图像的 AngularJs 服务。这是我的服务: /*global angular, Image*/ (function () { 'use strict'; f
随着数据库大小的增加,我有一个查询需要更长的时间来执行。查询已优化并且是必要的,但我的 C# 控制台应用程序最近给我这个错误: Unhandled Exception: MySql.Data.MySq
我正在研究 Linux shell 中的 timeout 命令。 当我尝试 timeout 1 bash 时,bash 将运行并在 1 秒后终止。 当我尝试 timeout 2 timeout 1 y
随着数据库大小的增加,我有一个查询需要更长的时间来执行。查询已优化并且是必要的,但我的 C# 控制台应用程序最近给我这个错误: Unhandled Exception: MySql.Data.MySq
我希望使用 Spring boot 和 Tomcat 扩展 Spring MVC 应用程序中的用户 session 。查看文档似乎有 2 个相关 properties : server.servlet
我正在尝试升级 Puppet 以使用 Ruby 1.9,但遇到了常量问题。 const_defined?("Timeout") 返回真,即使 :Timeout 不在常量列表中。这不会发生在 Ruby
首先,这是一个几乎重复的: How to differentiate when wait(long timeout) exit for notify or timeout? 但这是一个新的后续问题。
对于下面的代码,notifyAll() 会一直持有锁直到完成,即使超时了,这个 block 也不持有锁,必须等待notifyAll() block 完成。那么wait(timeout)中的timeou
Thread.Sleep(timeout) 和resetEvent.Wait(timeout) 都会导致执行暂停至少timeout 毫秒,那么它们之间有区别吗?我知道 Thread.Sleep 导致线
我是一名优秀的程序员,十分优秀!