Professor Zero
Professor Zero

Reputation: 35

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

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

var timeinmilliseconds = 60000;
var reloadCnt = window.sessionStorage.getItem("reloadCounter") ? parseInt(window.sessionStorage.getItem("reloadCounter")) + 1 : 1;

window.sessionStorage.setItem("reloadCounter", reloadCnt);
console.log(reloadCnt);
if (reloadCnt <= var x = document.getElementById('a').value;)
  setTimeout(function() { window.location.reload(true) }, timeinmilliseconds);
<form action="index.php" 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>

what am i doing wrong in this code?everything is working correctly but its doesnt refresh the page. plus if i set the code to

if ( reloadCnt <= 3 )

it works.and if i set it to

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

it doesnt works. please help

Upvotes: 1

Views: 725

Answers (3)

Edward D
Edward D

Reputation: 540

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>

Upvotes: 1

Someone Special
Someone Special

Reputation: 13588

Can u try using this instead.

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

Upvotes: 0

Matt Morgan
Matt Morgan

Reputation: 5303

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)) { ... }

Upvotes: 0

Related Questions