带有参数的JavaScript倒计时

时间:2016-06-15 17:09:54

标签: javascript ruby-on-rails

您将获得一个名为start_num的整数。编写一个从start_num倒计时到1的代码,当倒计时结束时,将打印出“Liftoff!”。

我不确定如何做到这一点并继续陷入困境。

这是我在问题开始时提供的代码:

    function liftoff_countdown(start_num) {
    // My code goes here!
    }

然后他们希望我传入一个像5:

这样的值
   liftoff_countdown(5);

然后这将是我的输出:

6

4
3
2
1
“起飞!”

谢谢!

4 个答案:

答案 0 :(得分:1)

看看这可能有助于您创建自己的代码

在同一个文件夹(script.js和index.html)中创建两个文件

<强>的index.html

<!doctype html>
<head>
  <title>Countdown</title>
</head>
<body>
  <div id="container">
  <div id="inputArea">
  </div>
  <h1 id="time">0</h1>
</div> 
<script src="script.js"></script>
</body>
</html>

<强>的script.js

var valueRemaining;
var intervalHandle;

function resetPage() {
  document.getElementById("inputArea").style.display = "block";
}

function tick() {
  var valueDisplay = document.getElementById("time");
  valueDisplay.innerHTML = valueRemaining;
  if (valueRemaining === 0) {
    valueDisplay.innerHTML = "Liftoff!";
    clearInterval(intervalHandle);
    resetPage();
  }
  valueRemaining--;
}

function startCountdown() {
  var count = document.getElementById("count").value;
  if (isNaN(count)) {
    alert("Please enter a number!");
    return;
  }
  valueRemaining =  count;
  intervalHandle = setInterval(tick, 1000);
  document.getElementById("inputArea").style.display = "none";
}

// as soon as the page is loaded...
window.onload =  function () {
  var inputValue = document.createElement("input");
  inputValue.setAttribute("id", "count");
  inputValue.setAttribute("type", "text");
  // create a button
  var startButton = document.createElement("input");
  startButton.setAttribute("type", "button");
  startButton.setAttribute("value", "Start Countdown");
  startButton.onclick = function () {
      startCountdown();
  };
  // add to the DOM, to the div called "inputArea"
  document.getElementById("inputArea").appendChild(inputValue);
  document.getElementById("inputArea").appendChild(startButton);
};

在这个例子中,你有很多东西要理解javascript如何在幕后工作。

答案 1 :(得分:0)

这个怎么样......

function liftoff_countdown()
{
  var span=document.getElementById('num');
  var i=document.getElementById('num').innerText;
  i=i-1;
  span.innerText=i;
  if (i==0){
   span.innerText='Liftoff!';
   clearInterval(count_down)
  }
 
}
var count_down=setInterval(liftoff_countdown,1000);
<span id="num">5</span>

答案 2 :(得分:0)

您可以使用简单的递归函数实现此目的,并使用setTimeout1 second经过一段时间后递归调用函数。

function lift_off(seconds) {
  if(seconds == 0) {
    console.log('liftoff');
  } else {
    console.log(seconds--);
    setTimeout(function(){lift_off(seconds);},1000);
  }
}

lift_off(10);

这是一个有效的JSFiddle

答案 3 :(得分:0)

<强>前言

很多这些答案似乎都集中在用定时器和递归来做事情上。我不相信这是你的意图。如果您的唯一目标是将这些值打印到控制台,则可以执行以下操作(请参阅注释以获得解释)。

答案

function liftoff_countdown(start_num) {
    // Loops through all values between 0 and start_num
    for(int i = 0; i < start_num; i++) {
        // Prints the appropriate value by subtracting from start_num
        console.log( start_num - i );
    }
    // Upon exiting the loop, prints "Liftoff!"
    console.log("Liftoff!");
}

其他想法

您可以轻松地向后循环数字,而不是向前循环:

for(int i = start_num; i > 0; i--){
    console.log( i );
}

我倾向于倾向于向前迭代,因为它更常见,如果他们掩盖了循环初始化,那么通常很容易混淆代码的读者。

此外,我正在假设当你说&#34; print&#34;你是说&#34; console.log()&#34;。如果这是不真实的,你当然可以使用任何其他功能(例如alert( "Liftoff!" );)。