为什么我的按钮不触发计时器?

时间:2018-09-07 05:35:25

标签: javascript jquery datetime button

我找到了一个非常简单的Javascript示例,以了解时间跟踪器的工作方式。我几个小时以来一直在比较代码,但我没有运行它。时钟的更新正在运行,但是该按钮不会触发时间跟踪器。

我的错误在哪里?

下面是示例JS代码。时钟根据定义的“ setInterval()”定期更新。我只有在按下按钮后才能找到触发时钟开始的错误。

$(function() {
  registerClock();
  setTime();
});

var currentdate;
var stopWatchRunning = false;
var startTime;

function registerClock() {
  setInterval(updateClock, 250);
}

function updateClock() {
  setTime();
  setStopWatch();
}



function setTime() {
  currentdate = new Date();
  var datetime = +currentdate.getDate() + "." +
    (currentdate.getMonth() + 1) + "." +
    currentdate.getFullYear() + " " +
    currentdate.getHours() + ":" +
    currentdate.getMinutes() + ":" +
    currentdate.getSeconds();
  $("#time").text(datetime);
}


$("#startstop").click(function() {
  if (stopWatchRunning == false) {
    startTime = new Date();
    stopWatchRunning = true;
  } else {
    stopWatchRunning = false;
  }
});


function setStopWatch() {
  if (stopWatchRunning == false) {
    return;
  }
  var timeElapsed = currentdate - startTime;
  var duration = new Date(timeElapsed);
  var showDuration = duration.getHours() - 1 + ":" +
    duration.getMinutes() + ":" +
    duration.getSeconds();
  $("#tracker").text(showDuration);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="js/timer_neu.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<nav class="navbar navbar-default">
  <div class="container-fluid">
    <div class="navbar-header">
      <a class="navbar-brand" href="#">Stoppuhr</a>
    </div>
  </div>
</nav>
<div class="row">
  <div class="col-md-5">
    <h1>Uhrzeit</h1>
    <h1 id="time">19:53:00</h1>
  </div>

  <div class="col-md-5">
    <h1>Stoppuhr</h1>
    <h1 id="tracker">00:00:00</h1>
  </div>

  <div class="col-md-2">
    <p><button id="startstop" class="btn btn-default">Start/Stop</button></p>
    <div>
    </div>

1 个答案:

答案 0 :(得分:1)

如下所示,将$(“#startstop”)单击函数移至文档准备功能内

for field in temp_result.schema.fields:
    if str(field.name) not in ['customerid', "overall_count", "overall_amount"]:
        name = str(field.name)
        temp_result = temp_result.withColumn(name, \
                                             when(col(name) >= 1, 1).otherwise(0))