访问动态增量变量JS

时间:2017-10-21 13:33:18

标签: javascript

我正在玩JS中的小游戏。 有一个日期计数器,每隔一秒递增setInterval()。 我想从主daycount函数中访问增量变量new_game(),以根据已经过去的天数触发游戏事件。

如果有人可以帮助或指出我正确的方向,我将不胜感激。

var day = $('#time');
var daycount = 0;

$(document).ready(function () {
    $('#start').click(function () {
        new_game();
        $(this).addClass('disabled').html('Started');
    })
});

function time() {
    setInterval(function () {
        daycount++;
        day.html('Day ' + daycount);
        return daycount;
    }, 1000);
}

function new_game() {
    time();

    if(daycount == 5){
        alert('something');
    }

}

3 个答案:

答案 0 :(得分:1)

def remove_abbreviations(abbreviation): list(abbreviation) words = [abbreviation.replace('Mr.', 'Mr') for w in len(abbreviation)] daycount

应该是count而不是day.html('Day ' + daycount);

var day = $('#time');
var daycount = 0;

$(document).ready(function () {
    $('#start').click(function () {
        new_game();
        $(this).addClass('disabled').html('Started');
    })
});

function time() {
    setInterval(function () {
        daycount++;
        day.html('Day ' + daycount);
        if(daycount == 5){
            alert('something');
        }
        //return daycount;
    }, 1000);
}

function new_game() {
  console.log("new_game called ");
    time();
    
    

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="time">...</div>
<div id="start">Click me </div>

答案 1 :(得分:1)

您可以将回调函数传递给time()

您无法返回setTimeout回调内部,因为没有任何内容可以返回。

同样现在,你只是在dayCount被初始化时才检查new_game() ....而不是每次在间隔计时器中更改

let daycount = 0,
  day = $('#day');

function time(callback) {
  setInterval(function() {
    daycount++;
    day.html('Day ' + daycount);
    callback(daycount);
  }, 1000);
}

function new_game() {
  function checkCount(daycount) {
    if (daycount == 5) {
      console.log('Is 5');
    } else {
      console.log('Not 5');
    }
  }
  time(checkCount);
}

new_game()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="day">

答案 2 :(得分:1)

除了要求的功能外:

    添加了
  • clearInterval()以防止setInterval()连续计数。
  • 添加了
  • <input>,因此用户可以设置结束计数。

详情在演示

中发表

演示

&#13;
&#13;
var day = $('#time');
var daycount = 0;

$(document).ready(function() {
  $('#start').on('click', function() {

    /* Assign counter as the timer() function
    || running every second.
    */
    var counter = setInterval(timer, 1000);

    /* Define timer() function to display the
    || daycount increments as the value of
    || output#time
    || When daycount reaches the value of user
    || input of input#end, run the 
    || stop() function
    */
    function timer() {
      daycount++;
      day.val('Day ' + daycount);
      if (daycount === Number($('#end').val())) {
        stop();
      }
    }

    /* Define stop() function
    || clearInterval of counter() function
    || thereby stopping the count from going 4eva
    || log the count
    */
    function stop() {
      clearInterval(counter);
      console.log('Day ' + daycount);
    }

    $(this).addClass('disabled').html('STARTED');
  });
});
&#13;
button,
input,
output {
  font: inherit
}

input {
  width: 5ch
}

.disabled {
  pointer-events: none
}
&#13;
<input id='end' type='number' min='0' max='999'>
<button id='start'>START</button>
<output id='time'></output>








<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;