<!doctype html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('MyController',
function($scope, $interval)
{ $scope.message = '現在時刻を表示';
var t;
// スタートボタンで1000ミリ秒単位に処理を実行
$scope.onStart = function()
{ t = $interval(function()
{ $scope.message = '現在時刻:' + (new Date()).toLocaleTimeString();
}, 1000);
}
// ストップボタンでタイマー処理を中止
$scope.onStop = function()
{ $interval.cancel(t);
};
});
</script>
</head>
<body ng-controller="MyController">
<div>{{message}}</div>
<button ng-click="onStart()">スタート</button>
<button ng-click="onStop()">ストップ</button>
</body>
</html>
|