search

Home  >  Q&A  >  body text

angular.js - Ask about the difference between $timeout and native setInterval

It’s very simple, I only put the key code

<p ng-controller="myCtrl">
<input type="text" ng-model="clock.now"></p>


function myCtrl($scope,$timeout){
$scope.clock = {
    now : new Date()
}
var updateClock = function(){
    $scope.clock.now = new Date() ;
}
//$timeout(function(){
setInterval(function(){    
    updateClock()
    $scope.$apply()
},1000)
updateClock();

}

You can use setInterval, but not $timeout. Why?

阿神阿神2774 days ago596

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-05-15 16:51:56

    Uh, $timeoutsetInterval 可不是一对儿呀,你要用也得用 $interval 才是……;$timeout 是对 setTimeout encapsulation.

    reply
    0
  • Cancelreply