【发布时间】:2020-04-29 12:54:51
【问题描述】:
我定义了两个 Date 类型的全局变量,并在函数中更改了值,但不知何故,在运行函数后,全局变量的值没有采用新值。问题是什么? 提前致谢!
var value1 = new Date()
var value2 = new Date()
var $scope = self.ctx
$scope.settimes = function() {
value1.setSeconds(0)
value1.setMinutes(15)
value1.setHours(6)
value2.setSeconds(0)
value2.setMinutes(30)
value2.setHours(12)
}
console.log(value1)
console.log(value2) //value1 and value2 still have the same value =new Date() but not be assigned with the values within the function
<button ng-click="settimes()">Change Time</button>
【问题讨论】:
-
对我来说,self.ctx 返回 undefined 所以脚本返回错误。您可以一次性设置时间值(也可以将毫秒数归零):
value1.setHours(6, 15, 0, 0).
标签: javascript html date momentjs