search
HomeWeb Front-endJS TutorialAngularJS timer cannot be stopped

1. Problem background

After setting the timer in AngularJS, click the button again to trigger the timer. At this time, it will appear: the previous timer has not stopped, and the next timer has not stopped. The timer is started again, resulting in multiple timers running at the same time.


##2. Problem source code

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>AngularJS之定时器无法停止</title>
		<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
		<script>
			var app = angular.module("btnApp",[]);
			app.controller("btnCon",function($scope,$interval){
				$scope.count = 0;
				$scope.getDayTime = function(){
					var date = new Date();
					var year = date.getFullYear();
					var month = date.getMonth()+1;
					var day = date.getDate();
					var hour = date.getHours();
					var minute = date.getMinutes();
					var second = date.getSeconds();
					var thisTime = year+"-"+(month<10?"0"+month:month)+"-"+(day<10?"0"+day:day)+" "+(hour<10?"0"+hour:hour)+":"+(minute<10?"0"+minute:minute)+":"+(second<10?"0"+second:second);
					
					return thisTime;
				};
				$scope.dayTime = $scope.getDayTime();
				$scope.queryData = function(){
					var timer = $interval(function(){  
	                    $scope.count++;  
	                    console.log($scope.count);
	                },4000);
	                
	                $scope.$on("destroy",function(){
					   $interval.cancel($scope.timer);
					}); 
				};
			});
		</script>
	</head>
	<body ng-app="btnApp">
		<p ng-controller="btnCon">
			<button ng-click="queryData();">查询</button>
			<p>
				<label>{{count}}</label>
			</p>
		</p>
	</body>
</html>


3. Question result



##4. Solved source code


5. Solved result

The above is the content that the AngularJS timer cannot be stopped. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在 iPhone 相机上设置定时器如何在 iPhone 相机上设置定时器Apr 14, 2023 am 10:43 AM

您可以在 iPhone 相机上设置多长时间的定时器?当您在 iPhone 的相机应用程序中访问定时器选项时,您将获得在两种模式之间进行选择的选项:3 秒 (3s)和10 秒 (10s)。当您手持 iPhone 时,您可以使用第一个选项从前置或后置摄像头快速自拍。第二个选项在场景中很有用,可以在远处将 iPhone 安装到三脚架上来点击合影或自拍。 如何在 iPhone 相机上设置定时器 虽然在 iPhone 相机上设置定时器是一个相当简单的过程,但具体操作方式因所使用的 iPhone 机型而异。

如何实现Workerman文档中的定时器功能如何实现Workerman文档中的定时器功能Nov 08, 2023 pm 05:06 PM

如何实现Workerman文档中的定时器功能Workerman是一款强大的PHP异步网络通信框架,它提供了丰富的功能,其中就包括定时器功能。使用定时器可以在指定的时间间隔内执行代码,非常适合定时任务、轮询等应用场景。接下来,我将详细介绍如何在Workerman中实现定时器功能,并提供具体的代码示例。第一步:安装Workerman首先,我们需要安装Worker

如何在Java中设置每日定时任务执行的定时器?如何在Java中设置每日定时任务执行的定时器?Dec 27, 2023 am 11:10 AM

Java定时器:如何设置每天定时执行任务?在日常的Java开发中,我们经常会遇到需要每天定时执行某个任务的需求。比如说每天凌晨1点执行数据备份任务,或者每天晚上8点发送日报邮件等等。那么在Java中,我们可以使用定时器来实现这样的功能。Java提供了多种定时器的实现方式,本文将介绍基于Timer和ScheduledExecutorService两种方式来设置

java定时器表达式是什么java定时器表达式是什么Dec 27, 2023 pm 05:06 PM

定时器的表达式用于定义任务的执行计划。定时器的表达式是基于“在给定的时间间隔之后执行任务”的模型。表达式通常由两个部分组成:一个初始延迟和一个时间间隔。

2022年最新5款的angularjs教程从入门到精通2022年最新5款的angularjs教程从入门到精通Jun 15, 2017 pm 05:50 PM

Javascript 是一个非常有个性的语言. 无论是从代码的组织, 还是代码的编程范式, 还是面向对象理论都独具一格. 而很早就在争论的Javascript 是不是面向对象语言这个问题, 显然已有答案. 但是, 即使 Javascript 叱咤风云二十年, 如果想要看懂 jQuery, Angularjs, 甚至是 React 等流行框架, 观看《黑马云课堂JavaScript 高级框架设计视频教程》就对了。

定时器的工作原理是什么定时器的工作原理是什么Aug 16, 2023 pm 02:18 PM

定时器的工作原理可以分为硬件定时器和软件定时器两种类型。硬件定时器的工作原理是时钟信号源提供稳定的时钟信号作为计时器的基准。计数器从预设值开始计数,每当时钟信号到达时计数器递增。当计数器达到预设值时,定时器会触发一个中断信号通知中断控制器处理相应的中断服务程序。在中断服务程序中,可以执行一些预定的操作。软件定时器的工作原理是通过编程语言或系统提供的库函数或系统调用来实现的等等。

掌握Go语言文档中的time.NewTimer函数实现单次定时器掌握Go语言文档中的time.NewTimer函数实现单次定时器Nov 03, 2023 pm 02:19 PM

掌握Go语言文档中的time.NewTimer函数实现单次定时器,并附上具体代码示例。时间作为我们生活的基准,定时器是编程中非常常用的工具之一。在Go语言中,我们可以使用time包来处理时间相关的操作,其中NewTimer函数可以用于创建一个单次定时器。本文将介绍如何使用NewTimer函数来实现一个简单的单次定时器,并附上具体代码示例。在Go语言中,tim

如何在Java中设置定时执行每月任务?如何在Java中设置定时执行每月任务?Jan 11, 2024 pm 04:50 PM

Java定时器:如何设置每月定时执行任务?引言:在开发中,经常会遇到需要每月定时执行任务的场景,例如每月更新统计数据、定期发送报表等。Java提供了多种定时器实现方式,本文将介绍如何使用Java定时器来实现每月定时执行任务,并提供具体的代码示例。一、使用Timer类实现每月定时执行任务Timer类是Java提供的最基础的定时器类,通过它可以实现简单的定时任务

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment