计数器的中间值(intermediate values of counter)

编程入门 行业动态 更新时间:2024-10-15 06:16:29
计数器的中间值(intermediate values of counter)

请帮助修复脚本。 http://jsfiddle.net/k9r4t/

在盘旋时 button_hover增加计数器的值。 但是当光标离开时,计数器的值显示在屏幕上。 button_hover。 并且用户只能看到计数器的初始值和最终值。

我希望用户看到中间值

控制器代码:

var app = angular.module("moduleCounter", []); app.controller("controllerCounter", function ($scope){ $scope.counter = 0; $scope.incrementCounter = function(){ $scope.startNext = setInterval(function(){ $scope.counter++; }, 400); } $scope.stopCounter = function(){ clearInterval($scope.startNext); } });

please help to fix the script. http://jsfiddle.net/k9r4t/

when hovering. button_hover increasing the value of the counter. but the value of the counter is displayed on the screen when the cursor leaves the. button_hover. and the user sees only the initial and final value of the counter.

I would like the user to see the intermediate values

controller code:

var app = angular.module("moduleCounter", []); app.controller("controllerCounter", function ($scope){ $scope.counter = 0; $scope.incrementCounter = function(){ $scope.startNext = setInterval(function(){ $scope.counter++; }, 400); } $scope.stopCounter = function(){ clearInterval($scope.startNext); } });

最满意答案

使用$timeout ,它是Angular的本机服务:

var app = angular.module("moduleCounter", []); app.controller("controllerCounter", function ($scope , $timeout){ $scope.counter = 0; $scope.incrementCounter = function(){ $scope.setTimeout(); } $scope.setTimeout = function(){ $scope.startNext = $timeout( $scope.counterUp , 400); } $scope.counterUp = function(){ $scope.counter++; $scope.setTimeout(); } $scope.stopCounter = function(){ $timeout.cancel($scope.startNext); } });

JSFiddle: http : //jsfiddle.net/cherniv/k9r4t/1/

Use $timeout , it is Angular's native service:

var app = angular.module("moduleCounter", []); app.controller("controllerCounter", function ($scope , $timeout){ $scope.counter = 0; $scope.incrementCounter = function(){ $scope.setTimeout(); } $scope.setTimeout = function(){ $scope.startNext = $timeout( $scope.counterUp , 400); } $scope.counterUp = function(){ $scope.counter++; $scope.setTimeout(); } $scope.stopCounter = function(){ $timeout.cancel($scope.startNext); } });

JSFiddle: http://jsfiddle.net/cherniv/k9r4t/1/

更多推荐

本文发布于:2023-08-07 17:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计数器   intermediate   counter   values

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!