Does anyone here know how i could modify this java script code which automatically refresh a page due to the time interval you set.

I need assistance on how i can modify this code so it refreshes only once when the page is opened,

<script>
<!--

/*
Auto Refresh Page with Time script
By JavaScript Kit (javascriptkit.com)
Over 200+ free scripts here!
*/

//enter refresh time in "minutes:seconds" Minutes should range from 0 to inifinity. Seconds should range from 0 to 59
var limit="0:30"

if (document.images){
var parselimit=limit.split(":")
parselimit=parselimit[0]*60+parselimit[1]*1
}
function beginrefresh(){
if (!document.images)
return
if (parselimit==1)
window.location.reload()
else{ 
parselimit-=1
curmin=Math.floor(parselimit/60)
cursec=parselimit%60
if (curmin!=0)
curtime=curmin+" minutes and "+cursec+" seconds left until page refresh!"
else
curtime=cursec+" seconds left until page refresh!"
window.status=curtime
setTimeout("beginrefresh()",1000)
}
}

window.onload=beginrefresh
//-->
</script>

Recommended Answers

All 10 Replies

What are you trying to achieve?
I can understand reloading a page every x seconds, but why would you display a page and then x seconds later reload the page just once?

i want to be able to auto update a picture profile which i normally use refresh button to acieve

Sorry, I'm still not understanding. Can you take me through what you want to happen step-by-step. No code.

Do you mean the picture is being uploaded without a postback and you want the picture to be updated automatically on the page once the upload is complete?

More info is needed, I agree with paulkd that you may want to tell us more about the process itself.

When i update my profile picture, i always need to click on refresh to get the new update, what i wanted to do is make the script just reload once when rediredted to the page so i donr need to click on refresh agai, this script works but it keeps refreshing in loop, i just need it to refresh once when the page is visited

Ok, so your problem is that you cant really track the number of times a page has been refreshed because anything you store in a javascript variable will be lost when the page is reloaded.

You can store a value in a cookie, or HTML5 localstorage.

There's another simplier option which will work the first time the page is opened. You can assign a value to window.name and that value can be accessed after a page reload unless the tab or window is closed..

here is an example you can try and you will see it will only reload the page 1 time after one second.

<!doctype html>
<html>
<head>
<script>
function beginrefresh(){
  window.name = "refreshed"
  var x = window.name
  if (x != "refreshed") {
  window.location.reload()
  } else {
  document.write("Reloaded 1 time..")
  }    
} 
setTimeout("beginrefresh()",1000);
</script>
</head>
<body>

</body>
</html>

Again, a better, more appropriate solution may be to save a value to a cookie and just read the cookie after the page refresh. If the value is there, do not reload the page again.

Same thing if you are interested in using HTML5 local storage, but with the local storage option, only the newer versions of browsers support it.

i have tried all you said but cant get it to work, can anyone help with a working example

The example i posted is a working example based on your description.

What isn't working about the example provided? It reloads the page one time after one second.

i assign a value to window.name 1, what do i put here document.write("Reloaded 1 time..") in Reloaded 1 time..

i assign a value to window.name 1

If you did that then you have to check to see if the value is 1 during the if..else statement.

what do i put here document.write("Reloaded 1 time..") in Reloaded 1 time..

nothing is required there... the point of that in my example was to show that the page was reloaded and the if..else statement's condition was FALSE when it ran on the second time, after the page refresh showing that the window.name value was still accessible and from that point on the statement would always be FALSE and the page would no longer reload.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.