0

I want to auto refresh a page after every certain period of time which will be provided by the user.I have made the code which is reloading the page only once.what may be the problem?

Here is my code:

function refreshmethod(){ var myvalue = document.getElementById('mytextBox').value; if(myvalue <= 0){ alert('Please enter a positive value in the input'); return; } setInterval(function(){ window.location.reload(); }, myvalue * 1000); } 
6
  • 1
    How do you call your function refreshMethod ? Commented Oct 26, 2017 at 7:21
  • 2
    after reloading first time page, thie mehod how is called again? refreshmethod Commented Oct 26, 2017 at 7:21
  • 4
    Also store that value somewhere (like localStorage), otherwise it will be wiped every time page is reloaded. Commented Oct 26, 2017 at 7:23
  • Also it's reloading the page, which resets the code execution (it's basically a new page with blank values). setTimeout would be better suited. What would you like to have refreshed? ajax is invented for this kind of stuff. Commented Oct 26, 2017 at 7:26
  • you need to make var myvalue = document.getElementById('mytextBox').value; as static otherwise it will get undefined when the page refreses. Commented Oct 26, 2017 at 7:28

3 Answers 3

3

When you reload the page, any scripts that were running during the previous load are stopped, so setInterval functions no longer continue.

You can save the refresh period in sessionStorage. When the page loads, it can check to see if this is set, and schedule another reload.

function auto_refresh() { var saved_refresh = sessionStorage.getItem("refresh"); if (saved_refresh) { setTimeout(function() { window.location.reload(); }, saved_refresh * 1000); } } window.onload = auto_refresh; function refreshmethod(){ var myvalue = document.getElementById('mytextBox').value; if(myvalue <= 0){ alert('Please enter a positive value in the input'); return; } setTimeout(function(){ window.location.reload(); }, myvalue * 1000); sessionStorage.setItem("refresh", myValue); } 
Sign up to request clarification or add additional context in comments.

Comments

0
function refreshmethod(){ var myvalue = document.getElementById('mytextBox').value; if(myvalue <= 0){ alert('Please enter a positive value in the input'); return; } if(!localStorage.getItem("item1")=“go”){ localStorage.getItem("item1",”go”); } if(localStorage.getItem("item1",”go”)){ setInterval(function(){ window.location.reload(); }, myvalue * 1000); } } 

I use my mobile so may be error with this code check it but the whole idea is to use Local storage set and get

Comments

0

A tinier solution:

(unfortunately, for understandable security reasons, we can't build a code snippet…)

 function refreshMethod(every){ console.log(every); if (every <= 0) { return alert('Please enter a positive value in the input') } sessionStorage.setItem('laps', every); setInterval(function(){ window.location.reload(); }, every * 1000); } try { refreshMethod(sessionStorage.getItem('laps')) } catch(error){} 
<p>Input a value and exit the field (with tab)</p> <input type="text" name="mytextBox" value="" id="mytextBox" onchange="refreshMethod(this.value)"> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.