如何在if语句中调用函数--javascript

时间:2014-04-19 22:09:07

标签: javascript jquery function

我有一个间隔设置,当有人点击一个名为"重置"的按钮时,我想要清除/结束。这样,开始间隔的功能就是新鲜的,可以接受新值。

如何做到这一点?

这是我的代码:

<div class="grid">
  <div id="circle" class="center">
    <div id="timer" class="fl"></div>
      <div class="grid">
        <div class="col-1-2 center">
          <div class="circleSelect">
            <select id="min" class="fm">  
              <option value="0">0 minutes</option>
              <option value="300">5 minutes</option>
              <option value="600">10 minutes</option>
              <option value="900">15 minutes</option>
              <option value="1200">20 minutes</option>
              <option value="1500">25 minutes</option>
            </select>
          </div>
        </div>
      </div>
    </div>
</div>
<div class="grid">
  <div class="col-1-3 center centerText prZ ptm">

    <div id="begin">
    <a class="btn fm" onClick="begin()">Begin Meditation</a>
    </div>

    <div id="during" class="opacity">
    <a class="btn fm black" onClick="stop()">Pause</a>
    <a class="btn fm red" onClick="resetTime()">Reset</a>
    <a class="btn fm red" onClick="finish()">Finish Test</a>
    </div>

    <div id="finish" class="centerText ptm grid opacity">
      <div id="quote" class="pbm col-1-2 center">
          <p class="centerText fm">"Do not dwell in the past, do not dream of the future, concentrate the mind on the present moment."
           <p class="fm">- Buddha</p>
          </p>
        </div>
        <a id="again" class="btn fm" onClick="again()">Meditate Again</a>

    </div>

  </div>

  <audio id="soundName" style="display: none;"></audio>
</div>
<script>

  function begin() {

    var min = parseInt($('#min').val(), 10);
    var sec = 1;
    var time = min + sec; 

    setInterval( function() {
      if (time > 0){
        --time;
      }
      else if(time == 0){
        time = -1;
        finish();
      }
    }, 1000);

    function convertTime() {
        var mins = Math.floor((min % 3600) / 60);
        var secs = (sec % 60);
        // return humanTime;

        if (mins >= 0){
        var timeTravel = setInterval( function() {
          --secs;

        if (secs == -1 && min > 0){secs = 59; --mins;}  // changes the minute 
        else if (secs == 0 && min == 0) {clearInterval(timeTravel);} // stops at 00:00


        var humanTime = (((mins < 10) ? "0" : "") + mins + ":" + ((secs < 10) ? "0" : "") + secs);
        $('#timer').html(humanTime).fadeIn(200);
        }, 1000); 
      } else {}

    }

    convertTime();

    $('#begin').fadeToggle(200);
    $('.selectStyle').fadeToggle(200);
    $('.circleSelect').fadeToggle(200);
    $('#during').delay(200).fadeToggle(400);

  }

  function stop() {

  }


  function resetTime(interval) {
    $('#during').fadeToggle(200);
    $('#begin').delay(200).fadeToggle(400);
    $('.selectStyle').fadeToggle(200);

    var interval = clearInterval(timeTravel);

    resetTime();
  }


  function finish() {


    $('#during').fadeOut(200);
    $('#circle').fadeOut(500);

    $('#finish').insertAfter('#circle').delay(1000).fadeToggle(400);
  }

  function again() {
    $('#finish').fadeOut(200);
    $('#begin').delay(200).fadeToggle(200);
    $('#circle').delay(200).fadeToggle(400);
    $('.selectStyle').fadeToggle(200);

  }


</script>

3 个答案:

答案 0 :(得分:1)

问题是timeTravelconvertTime的本地,因此timeTravel内的resetTime指的是不同的(在这种情况下,未定义)变量。

将其声明移到该函数之外,以便两个函数都可以访问它,并在这些函数中删除该变量的var

<script>
var timeTravel;

function convertTime() {
    // ...
    timeTravel = setInterval( /* ... */ );
    // ...
}

function resetTime() {
    // ...
    clearInterval(timeTravel);
    // ...
}
</script>

答案 1 :(得分:0)

您已将timeTravel变量声明为convertTime函数内的本地变量,这意味着当您退出函数时它会消失。

begin函数之外声明变量,并将赋值保留在convertTime函数内。这样您就可以使用resetTime函数中的变量来停止间隔。

答案 2 :(得分:0)

timeTravel()应该在全球范围内。

function convertTime() {
        if (/* code */){
            window.timeTravel = setInterval( function(/* code */) {
        }
}

var timeTravel = '';
function convertTime() {
        if (/* code */){
            timeTravel = setInterval( function(/* code */) {
        }
}