Forum Moderators: coopster

Message Too Old, No Replies

PHP timed execution

         

andrewsmd

4:34 pm on Oct 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does anyone know how I can execute a function or refresh a page at a certain time?
What I mean is I have a form that I use and have open for most of the day. I have functions that I call within HTML to output certain things. Something along the lines of this.
<html>
<?php
function doSomething(){
if($somevalue){
echo("some variable");
}//if
else{
echo("a different variable");
}//else
}//doSomething
?>
<body>
<table>
<tr>a table row</tr>
<tr><?php doSomething(); ?></tr>
</table>
</body>
</html>
Now let's say that when I open the page that $somevalue is not true so it echos a different variable
but maybe in two hours somevariable is true and I would like to run that again without having to manually refresh my page. I could run it at some time or on the variable change. Any suggestions?

cameraman

12:38 am on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You would need to use javascript, or there's an addon for firefox that will do a periodic refresh.

andrewsmd

7:15 pm on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok so I got it with some JS here is the code. This is an external JS that refreshes every 5 min and keeps your current spot when you hit refresh.

I really just pieced this together from other sites

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

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)

}//else

}//beginrefresh

var RecoverScroll=/*28432953204368616C6D657273*/
{

timer:null, x:0, y:0,cookieId:"RecoverScroll", dataCode:0, DEBUG:false,

init:function(pageName)
{
var offsetData;

if( document.documentElement )
this.dataCode=3;
else
if( document.body && typeof document.body.scrollTop!='undefined' )
this.dataCode=2;
else
if( typeof window.pageXOffset!='undefined' )
this.dataCode=1;

if(pageName)
this.cookieId=pageName;

if((offsetData=this.readCookie(this.cookieId))!=""
&& (offsetData=offsetData.split('¦')).length==4
&& !isNaN(offsetData[1]) && !isNaN(offsetData[3]))
{
window.scrollTo(offsetData[1],offsetData[3]);
if(this.DEBUG)
document.title=offsetData;
}

this.record();
this.addToHandler(window,'onscroll',function(){RecoverScroll.reset()});
},

reset:function()
{
clearTimeout(this.timer);
this.timer=setTimeout("RecoverScroll.record()",500);
},

record:function()
{
var cStr;

this.getScrollData();

this.setTempCookie(this.cookieId, cStr='x¦'+this.x+'¦y¦'+this.y);

if(this.DEBUG)
window.status=cStr;
},

setTempCookie:function(cName, cValue)
{
document.cookie=cName+"="+cValue;
},

readCookie:function(cookieName)
{
var cValue="";

if(typeof document.cookie!='undefined')
cValue=(cValue=document.cookie.match(new RegExp(cookieName+'=([^;]+);?'))) ? cValue[1] : "";

return cValue;
},

getScrollData:function()
{
switch( this.dataCode )
{
case 3 : this.x = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);
this.y = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
break;

case 2 : this.x=document.body.scrollLeft;
this.y=document.body.scrollTop;
break;

case 1 : this.x = window.pageXOffset; this.y = window.pageYOffset; break;
}
},

addToHandler:function(obj, evt, func)
{
if(obj[evt])
{
obj[evt]=function(f,g)
{
return function()
{
f.apply(this,arguments);
return g.apply(this,arguments);
};
}(func, obj[evt]);
}
else
obj[evt]=func;
}
}

window.onload=beginrefresh;
RecoverScroll.addToHandler(window,'onload',function(){RecoverScroll.init("yourPage.php")});

Tada!

eelixduppy

8:03 pm on Oct 28, 2008 (gmt 0)



You might want to consider a meta refresh, also, as this might be easier and doesn't rely on javascript.

andrewsmd

8:36 pm on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can I keep the same spot in the page with a meta refresh?

eelixduppy

8:39 pm on Oct 28, 2008 (gmt 0)



Not sure. I feel like that may be browser specific. I haven't tested it myself, though.

Receptional Andy

8:43 pm on Oct 28, 2008 (gmt 0)



Some browsers will keep the page position for any refresh (I know firefox does, I don't think this happens in IE). You're unlikely to get wide browser support though, although to me it feels intuitively like a browser issue, rather than a web developer's.

andrewsmd

9:06 pm on Oct 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do agree with you, my JS works in IE and FF. This is a company specific form so I can make my user's use one of those or lock them out. It's nice to have the power of a tech guy. Thanks for all of the help.