javascript - 带有 for 循环的倒数计时器

标签 javascript loops for-loop

我正在制作番茄钟以帮助更好地使用 Javascript。到目前为止,我已经设法将用户的输入转换为分钟,但我似乎无法使用 console.log 创建倒数计时器。

警告:它似乎会破坏浏览器

这是我的 fiddle :http://jsfiddle.net/tmyie/YcBh9/

Javascript:

        var doc = document,
            timer = doc.getElementsByClassName('timer')[0],
            button = doc.getElementsByTagName('button')[0];

        button.onclick = function () {
            var input = doc.getElementById('input').value;
            var speed = (input * 1000) * 60;

            for (i = speed; speed > 1; i - 1000) {

                console.log(i);
            }
        }

HTML:

<div class="timer"></div>I am going to spend
<input id="input"></input>minutes working
<button>Go!</button>

最佳答案

您的 for 循环没有正确递减,导致无限循环。 你需要 -= 而不仅仅是 -,而且你在检查 speed 而你应该检查 i

for (i = speed; i > 1; i -= 1000) {

关于javascript - 带有 for 循环的倒数计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17393622/

相关文章:

javascript - textarea 未使用 jQuery.Serialize() 序列化

javascript - 脚本仅返回从 php 中的数据库中获取的第一个值的 id

c# - 内容页面中的正文加载处理程序

JQuery循环遍历元素,如果全部为空则返回false

javascript - 为什么我的所有数字都是重叠的?

ios - 在 Swift 中填充多维数组

php - 表单提交后生成引用ID

javascript - Tensorboard 未在 Google Chrome 中运行

r - Print() 函数不在循环/函数内打印变量(R 代码)

c - 为什么 while 循环不能正常工作?