使用JavaScript在条件下重新加载HTML页面

时间:2013-11-26 15:00:36

标签: javascript html

如果特定输入字段(pass_phrase)中的值在一定时间后没有更改,比如2分钟,我需要重新加载HTML页面,让我们说。我是JS的新手,下面的代码就是我到目前为止所提供的代码。但它不起作用。如果我输入一些值,页面永远不会重新加载。请帮助指出正确的解决方案或代码问题。感谢。

JS:

    var v1 = '', v2 = '';           
    function GetMyValues(){        
        setTimeout(function() {
            v1 = document.getElementById('pass_phrase').value;
        }, 1000*60*1);        
        setTimeout(function() {
            v2 = document.getElementById('pass_phrase').value;
        }, 1000*60*2);        
    }  

    if (v1 == v2 && v1 != '') {
       window.location="https://domain-name.com";
    } else {
       setTimeout('GetMyValues()', 1000*60*1);
    }

2 个答案:

答案 0 :(得分:0)

我相信您可以使用它来重新加载您的页面:

location.reload(); 

查看this文档。

答案 1 :(得分:0)

您只在页面加载时检查v1v2。你需要在需要时检查它(超时后):

var v1 = '', v2 = '';

function GetMyValues() {

    setTimeout(function () {
        v1 = document.getElementById('pass_phrase').value;
    }, 1000 * 60 * 1);

    setTimeout(function () {
        v2 = document.getElementById('pass_phrase').value;
        // Check and reload
        if (v1 == v2 && v1 != '') {
            window.location = "https://domain-name.com";
            // or location.reload(); if it is really a reload of the same page.
        } 
    }, 1000 * 60 * 2);

}

setTimeout(GetMyValues, 1000 * 60 * 1);

虽然这会在3秒后检查,但是1分钟后触发GetMyValues,然后等待2分钟来比较2个值。 如果要在页面加载后2分钟检查值,则应更改:

setTimeout(GetMyValues, 1000 * 60 * 1);

只是:

getMyValues();