简体   繁体   中英

updating time in angularjs app

In my angularjs app, i define a var for put a "prefix" in my all console.log

var app = angular.module('app', ['ngRoute', 'ngCookies', 'LocalStorageModule', 'ngResource', 'pascalprecht.translate']).run(function($rootScope, $timeout) {


    $rootScope.defineCLC = "[ZR Console CL] " + updatingTime() + " ===> ";

[etc ...]

I use $rootScope.defineCLC in my controllers.

For having the time updated, i put in pure js, outside :

function updatingTime() {
    setTimeout('updatingTime()', 3000);
    var currentTime = new Date();
    console.log('ok !');
    return currentTime;
}

problem is it's doesnt't work, the time is always the date when app whas executed :/ how to update the time for having it good in the console.log ?

setTimeout('updatingTime()', 3000); will give you an error after 3secs (check the developer console in the browser) since you supply a string instead of a function.

You'll have to make a function and call it with angular's version of setInterval .

var app = angular.module('app', []).run(function($rootScope, $interval) {
  var fun = function() {
    $rootScope.defineCLC = "[ZR Console CL] " + new Date() + " ===> ";
  };
  $interval(fun, 3000);
}]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM