备用运行计时器jquery

时间:2016-11-16 08:14:05

标签: javascript jquery

我想制作一个计时器,当空闲运行时,通话暂停,反之亦然,当我点击空闲时,空闲将恢复计数,反之亦然



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="col-md-12">
  <h1>TIMER
    <select id = "status">
      <option value="idle">Idle</option>
      <option value="talk">Talk_time</option>
    </select>
  </h1>
  <h3>Idle: <span id="ctr_idle"></span></h3>
  <h3>Talk_time: <span id="ctr_talk"></span></h3>
</div>
&#13;
#! /usr/bin/perl
use warnings;
use strict;
use feature 'say';

my $tmp = "rrccllrrc";

my %replace = ( r => 'right',
                c => 'center',
                l => 'left' );

$tmp =~ s/(.)/$replace{$1} /g;
chop $tmp;
say "<$tmp>";
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

试试这样:

var timer_talk ; var timer_idle;变量中添加了

global。 清除间隔不起作用。因为变量是私有的而不是全局的

var count_idle = $('#ctr_idle').text();
var count_talk = $('#ctr_talk').text();
var timer_talk ;//global
var timer_idle;//global
function clock_idle() {
  count_idle++;
  $('#ctr_idle').text(count_idle);
}

function clock_talk() {
  count_talk++;
  $('#ctr_talk').html(count_talk);
}

$('#status').change(function() {
  var choice = $('#status').val();
  console.log(choice);

  if (choice == 'talk') {
    clearInterval(timer_idle);
    timer_talk = setInterval(clock_talk, 1000);

  } else {
    clearInterval(timer_talk);
     timer_idle = setInterval(clock_idle, 1000);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="col-md-12">
  <h1>TIMER
    <select id = "status">
      <option value="idle">Idle</option>
      <option value="talk">Talk_time</option>
    </select>
  </h1>
  <h3>Idle: <span id="ctr_idle"></span></h3>
  <h3>Talk_time: <span id="ctr_talk"></span></h3>
</div>