I'm using the java script from How to automatically reload a page after a given period of inactivity to refresh my html page every 30 seconds, and it works great. The page contains a form, some buttons and some text fields.The form also contains a statuslist that is updated when the page is refreshed. When clicking on a button will the corresponding value be sent back to the server. The problem is that once I have clicked a button once, it is repeated every 30 second due to the refresh from the javascript. How to remove the button click after the first time? I want to keep the values in the text fields.
Asked
Active
Viewed 588 times
0
-
Added info about the statuslist, that is the reason for automatic reload of the page – Lars Ljungberg Mar 06 '15 at 12:44
-
are you having a handler defined ? are you adding any event listener to the button ? – Nielarshi Mar 07 '15 at 18:52
-
@ Nielarshi: Yes, I have an event listener for the button, but it seams like once the form is submitted, the values remains when doing a refresh. I have tested to change some of the text fields and they are restored by the refresh :( – Lars Ljungberg Mar 09 '15 at 10:51
3 Answers
1
If you have a function attached to your button like
<button onclick="myFunction()">Click me</button>
then add this line to that function
clickFlag = true
otherwise add this to your button tag
<button onclick="clickFlag = true">Click me</button>
and change your refresh function as following
var time = new Date().getTime();
$(document.body).bind("mousemove keypress", function(e) {
time = new Date().getTime();
});
function refresh() {
if(new Date().getTime() - time >= 30000 && clickFlag)
window.location.reload(true);
else
setTimeout(refresh, 10000);
}
setTimeout(refresh, 10000);

Capt Planet
- 113
- 6
-
The values in the text field will not remain when using reset. The refresh will only be done when clicking a button with the solution above, not every 30 second. Isn't there a way to clear just some values? – Lars Ljungberg Mar 06 '15 at 12:49
-
i've edited my answer, the page will only refresh if 30 seconds have passed without any activity and button hasn't been clicked. – Capt Planet Mar 07 '15 at 18:49
-
Isn't a keypress still needed for the page to reload as the clickFlag==true still is there ? – Lars Ljungberg Mar 09 '15 at 12:43
0
You can use meta tags . These attributes HTTP-EQUIV,content are needed. Ex: http://website/login">

caspersky 48
- 239
- 1
- 4
- 11
0
I found a solution, but not by using javascript. I added another html page, where I go to when submitting the form. Here I extract the data from the textfields (and call some php). Then I goes back to the first page usining the php header command, with the data attached but not the buttons. I still uses the javascript on the first page to update the statuslist, but don't get any duplicate button clicks due to the refresh.

Lars Ljungberg
- 313
- 1
- 6
- 19