How to refresh page with javascript for N number of times through user input?

后端 未结 3 1620
别那么骄傲
别那么骄傲 2021-01-25 18:11

i have coded a script that should refresh the page for Number of times the user input the value . here is the code

相关标签:
3条回答
  • 2021-01-25 18:41

    You have a some fundamental problems with your JavaScript. It's important that you understand why this is not working if you want to be able to use JS well in the future.

    This statement is not doing what you intend:

    if ( reloadCnt <= var x = document.getElementById('a').value; )
    

    You are saying "if reloadCnt is less than or equal to some thing" where "some thing" is the result of the statement:

    var x = document.getElementById('a').value;
    

    This statement is assigning document.getElementById('a').value to the variable x. The return value of such an assignment is always undefined in JavaScript. This means what you've effectively written is the same as:

    if ( reloadCnt <= undefined )
    

    This is always going to be false, which is why it's not working for you.

    There are two other issues:

    1. Your jQuery lookup will return a string, which you should convert to a number before comparing it to another number.

    2. You don't need a semicolon inside your conditional.

    In the end, this is probably what you want to write:

    if (reloadCnt <= Number(document.getElementById('a').value)) { ... }
    
    0 讨论(0)
  • 2021-01-25 18:42

    Can u try using this instead.

    if (reloadCnt <= document.getElementById('a').value;)
    
    0 讨论(0)
  • 2021-01-25 18:49

    I agree with Matt Morgan that your attempt to assign a variable inside a conditional:

    if ( reloadCnt <= var x = document.getElementById('a').value; )
    

    probably means you need to brush up on your understanding before proceeding.

    But here is a working demo for you. (Not having your index.php, which I assume populates the textbox "a", I've done it client-side and fetched the posted value from the querystring. Once you've got it working, you can always revert to document.getElementById('a').value)

    You'll also want to clear your sessionStorage variable when the user next hits the page, otherwise when you try to test it twice in a row, reloadCounter will still be where it was at the end of the last run.

    <form  method="GET">
     <p>Your link: <input type="text" rows="5" style="width:200px; height:50px;"  name="link" /></p>
     <p>number of times: <input type="text" id="a"  name="times" /></p>
    
     <p><input type="submit"  rows="5" style="width:200px; height:50px;" / ></p>
    </form>
    <script>   
    
    function getParameterByName(name) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }
        var timeinmilliseconds = 600;
        var reloadCnt = window.sessionStorage.getItem( "reloadCounter") ? parseInt(window.sessionStorage.getItem( "reloadCounter")) + 1 : 1;
        var times = getParameterByName('times');
        if (times)
        {
            window.sessionStorage.setItem( "reloadCounter", reloadCnt );
            console.log(reloadCnt + ' / ' + times);
            if ( reloadCnt <  times )
                setTimeout(function(){ window.location.reload(true) }, timeinmilliseconds);
        }
        else
        {
            window.sessionStorage.setItem( "reloadCounter", 0 );
        }
    </script>
    
    0 讨论(0)
提交回复
热议问题