我正在尝试构建计时器,但它无法正常工作。 我所做的是我有一个启动计时器的按钮。它设置开始时间,然后开始在屏幕上计数的间隔。但由于开始时间未定义,因此间隔调用的函数会失败。我试图看看是否正在设定开始时间,而且确实如此。但是当通过间隔调用它时,它是未定义的。
这是我的代码 一些属性
timeBegan = new Date();
started = null;
启动功能
start() {
this.timeBegan = new Date();
document.getElementById('ticker').innerHTML = '00:00:00.000';
this.started = setInterval(this.clockRunning, 10);
}
时钟运行功能
public clockRunning() {
const currentTime = new Date();
console.log(this.timeBegan);
console.log(currentTime.getTime());
const timeElapsed = new Date(currentTime.getTime() - this.timeBegan.getTime());
const hour = timeElapsed.getUTCHours();
const min = timeElapsed.getUTCMinutes();
const sec = timeElapsed.getUTCSeconds();
const ms = timeElapsed.getUTCMilliseconds();
console.log(currentTime.getSeconds());
console.log(this.timeBegan);
document.getElementById('ticker').innerHTML =
(hour > 9 ? hour : '0' + hour) + ':' +
(min > 9 ? min : '0' + min) + ':' +
(sec > 9 ? sec : '0' + sec) + '.' +
(ms > 999 ? ms : ms > 9 ? '0' + ms : '000' + ms);
}
我做错了什么?
答案 0 :(得分:2)
在启动功能中使用它:
class pointLocation {
var $pointOnVertex = true; // Check if the point sits exactly on one of the vertices?
function pointLocation() {
}
function pointInPolygon($point, $polygon, $pointOnVertex = true) {
$this->pointOnVertex = $pointOnVertex;
// Transform string coordinates into arrays with x and y values
$point = $this->pointStringToCoordinates($point);
$vertices = array();
foreach ($polygon as $vertex) {
$vertices[] = $this->pointStringToCoordinates($vertex);
}
// Check if the point sits exactly on a vertex
if ($this->pointOnVertex == true and $this->pointOnVertex($point, $vertices) == true) {
return "vertex";
}
// Check if the point is inside the polygon or on the boundary
$intersections = 0;
$vertices_count = count($vertices);
for ($i=1; $i < $vertices_count; $i++) {
$vertex1 = $vertices[$i-1];
$vertex2 = $vertices[$i];
if ($vertex1['y'] == $vertex2['y'] and $vertex1['y'] == $point['y'] and $point['x'] > min($vertex1['x'], $vertex2['x']) and $point['x'] < max($vertex1['x'], $vertex2['x'])) { // Check if point is on an horizontal polygon boundary
return "boundary";
}
if ($point['y'] > min($vertex1['y'], $vertex2['y']) and $point['y'] <= max($vertex1['y'], $vertex2['y']) and $point['x'] <= max($vertex1['x'], $vertex2['x']) and $vertex1['y'] != $vertex2['y']) {
$xinters = ($point['y'] - $vertex1['y']) * ($vertex2['x'] - $vertex1['x']) / ($vertex2['y'] - $vertex1['y']) + $vertex1['x'];
if ($xinters == $point['x']) { // Check if point is on the polygon boundary (other than horizontal)
return "boundary";
}
if ($vertex1['x'] == $vertex2['x'] || $point['x'] <= $xinters) {
$intersections++;
}
}
}
// If the number of edges we passed through is odd, then it's in the polygon.
if ($intersections % 2 != 0) {
return "inside";
} else {
return "outside";
}
}
function pointOnVertex($point, $vertices) {
foreach($vertices as $vertex) {
if ($point == $vertex) {
return true;
}
}
}
function pointStringToCoordinates($pointString) {
$coordinates = explode(" ", $pointString);
return array("x" => $coordinates[0], "y" => $coordinates[1]);
}
}
$pointLocation = new pointLocation();
$points = array("22.732965336387213 75.8609390258789");
$polygon = array("22.73549852921309 75.85424423217773","22.72346544538196 75.85561752319336","22.72346544538196 75.87175369262695","22.732332030848273 75.87295532226562","22.740406456758326 75.8686637878418","22.74198962160603 75.85407257080078");
echo '<pre>';
print_r($polygon);
// The last point's coordinates must be the same as the first one's, to "close the loop"
foreach($points as $key => $point) {
echo "point " . ($key+1) . " ($point): " . $pointLocation->pointInPolygon($point, $polygon) . "<br>";
}
或:
this.started = setInterval(this.clockRunning.bind(this), 10);
这肯定会解决你的问题。
答案 1 :(得分:1)
因为您正在引用它而无法正常工作:
this.timeBegan
但是,“this”并未引用您想要引用的“this”。 “this”是setInterval中定义的函数。这是因为ES5函数有自己的“this”。
您可以尝试使用ES6功能,该功能将使用您想要的“此”。
或者,在你的函数中强制“this”通过这样做来引用你想要的“this”:
this.clockRunning = this.clockRunning.bind(this)
这意味着clockRunning中的“this”将始终是传递给bind()的“this”。确保你传递正确的“this”来绑定:)即具有timeBegan属性的“this”。