Home > Web Front-end > JS Tutorial > body text

How to implement timer effect in Angular

php中世界最好的语言
Release: 2018-04-13 13:34:40
Original
2636 people have browsed it

This time I will show you how to achieve the timer effect in Angular. What are the precautions for Angular to achieve the timer effect? ​​The following is a practical case. Let’s take a look. one time.

This article introduces an example of a simple timer function implemented by Angular and shares it with friends in need.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title> Angular定时器</title>
  <script src="angular.min.js"></script>
  <script>
    var myapp=angular.module("myapp",[]);
    myapp.controller("myCtrl",function ($scope,$interval,$timeout) {
      $scope.num=0;
      $scope.fun=function () {
//        $interval(function () {
//          $scope.num++
//        },1000)
//        setInterval(function () {
//          $scope.$apply(function () {
//            $scope.num++;
//            console.log($scope.num)
//          })
//        },1000);
//                $interval(function () {
//
//            $scope.num++;
//            console.log($scope.num);
//
//        },1000,5,false);
        $timeout(function () {
          $scope.num++;
        },1000);
      }
    })
  </script>
</head>
<body ng-app="myapp" ng-controller="myCtrl">
<p>{{num}}</p>
<button ng-click="fun()">按钮</button>
</body>
</html>
Copy after login

Run the code and click the button to see the number Programming1 above the button after 1 second. Implemented a simple timing trigger function.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How Vue performs ajax request public method

Detailed explanation of the use of custom instructions in Angular17

The above is the detailed content of How to implement timer effect in Angular. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template