简体   繁体   English

将2个JavaScript合并为一个

[英]Merging 2 javascripts into one

I have 2 javascripts working but want to merge them into one. 我有2个javascript工作,但想将它们合并为一个。 The purpose of the script is to make a checkbox that changes the a varable for the refresh rate. 该脚本的目的是创建一个复选框,以更改刷新率的变量。

Script 1 (works as expected) 脚本1(按预期工作)

        window.onload = function () {
    var input = document.querySelector('input[type=checkbox]');

    function check() {
        if (input.checked) {
            var timeout = "10000";
        } else {
            var timeout = "999999";
        }
        document.getElementById('result').innerHTML = 'result ' + timeout;
    }
    input.onchange = check;
    check();
}

<input type="checkbox" value="1" />Checkbox
<br/>
<br/>
<span id="result"></span>

Script 2 (works as expected) 脚本2(按预期工作)

var timeout = 10000; 
var action = function() {
$('#content').load('/arduino/refresh');
}; 
setInterval(action, timeout);

I tought I could just merge them together and let them live happily ever after so I created the following script: (it shows the checkbox but checking/unchecking does not change the refresh rate) 我可以将它们合并在一起,然后让它们幸福地生活,因此我创建了以下脚本:(它显示了复选框,但选中/取消选中不会更改刷新率)

            window.onload = function () {
        var input = document.querySelector('input[type=checkbox]');

        function check() {
            if (input.checked) {
                var timeout = "10000";
            } else {
                var timeout = "999999";
            }
                var action = function() {
    $('#content').load('/arduino/refresh');
    }; 
        }
        input.onchange = check;
        check();
    }
setInterval(action, timeout);

<input type="checkbox" value="1" />Checkbox
<br/>
<br/>
<span id="result"></span>

Any advise? 有什么建议吗?

In the combined script, setInterval is not called within the check event handler. 在组合脚本中,在check事件处理程序中不会调用setInterval The local variable timeout is modified, but the new value is never used to actually change the refresh rate. 修改了本地变量timeout ,但是从未使用新值来实际更改刷新率。

Also, use clearInterval and setInterval together when changing the refresh rate. 另外,更改刷新率时,请同时使用clearIntervalsetInterval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM