我不知道为什么添加javascript计时器会破坏我的代码.昨天github提交失败44次



更新:我想感谢大家的努力帮助。我从来没有得到集成的代码片段,但这是我的错,不是你的:)我得到了一些javascript的帮助,我们不仅在bitvote上运行。但是我们已经有了第一次面试,twitter/ethercasts/status/477566828513099777,积极的社区反应,本周还有2次面试!^_^再次感谢stackexchange!

原始问题:

我试图插入4个javascript定时器到我的概念验证页面http://arkbg1.github.io/BitVote/

当用户点击"start here"按钮后,一个计时器以毫秒为单位显示"当前时间",然后是数据输入表,然后是代码日志。我需要它有4个定时器,而不是1个。

  1. 注册时间:MM/DD/YYYY hh: MM:ss(静态计数器,捕获用户点击的时间戳)。类似于w3schools(.)com/js/tryit.asp?filename=tryjs_date_gettime, MM/DD/YY hh: MM:ss格式除外。

  2. 当前时间:MM/DD/YYYY hh: MM:ss(实时计数)。类似于w3schools(.)com/js/tryit.asp?文件名= tryjs_timing_clock

  3. Spent Vote-Time: 00:00:00(静态计数器)。

  4. 可用投票时间:hh:mm:ss(实时计数器,点击后基本秒表开始)

每次我试图复制粘贴w3school代码到我的github代码,按钮/表脚本完全故障或消失。我昨天尝试了44次提交。没有工作,我不知道我在做什么来打破当前的代码或如何添加这4个基本定时器不打破页面。

这里是index.html和js.js - https://github.com/arkbg1/BitVote

我猜您正在尝试在文档完全加载之前使用document.getElementById抓取页面元素。尝试设置一个运行onload的"main"函数。您可以在JS文件中尝试这样做。然后,无论在哪里使用这些原始变量,都可以从state对象中获取它们。

var state;
window.onload = function() {
    state = {
        date: new Date(),
        spend_time: document.getElementById("spend_time"),
        button: document.getElementById("button"),
        spend_addr: document.getElementById("spend_addr"),
        power_time: document.getElementById("power_time"),
        progress: document.getElementById("progress"),
        passed: document.getElementById("passed"),
        to_fraction: document.getElementById("to_fraction"),
        amount_note: document.getElementById("amount_note"),
        spend_addr_note: document.getElementById("spend_addr_note"),
        old_spend_val: 0
    };
    voting(from_time() == 0);
    state.spend_time.value= state.old_spend_val;
    update_power_time();
    update_spend_addr();
};
<<p> 计时器例子/strong>
<!DOCTYPE html>
<html>
<head>
    <title>Timer Test</title>
    <script>
        window.onload = function () {
            var container = document.getElementById("container");
            setInterval(function () {
                var now = new Date(),
                    h = now.getHours(),
                    m = now.getMinutes(),
                    s = now.getSeconds();
                if (h < 10) h = "0" + h;
                if (m < 10) m = "0" + m;
                if (s < 10) s = "0" + s;
                container.innerHTML = h + ":" + m + ":" + s;
            }, 500);
        }
    </script>
</head>
<body>
    <div id="container">
    </div>
</body>
</html>

相关内容

最新更新