PDA

View Full Version : Auto refresh when no value on input box



l_kris06
12-31-2008, 05:58 PM
Hi All,

I need to refresh the page when there is no data in an input box, basically the input box used is "FCKeditor". When no data on the input box, the page should refresh every fixed/set interval. If data found, the page shouldnt refresh. There are several posts and code snippets on DD, but in my case, they arent help much.

I would appreciate if somebody could help me out, or give me some pointers that will help me get this resolved.

Best,
Kris A

???
12-31-2008, 06:13 PM
I think this does what you need.


<html>
<head>
<script type="text/javascript">
//Change these
var intervalT = 1000 //Time in milliseconds untill refresh
var elementId = "myBox" //ID of text box
//Don't change this crap
var ele;
window.onload = function () {
ele = document.getElementById (elementId);
setTimeout (function () {
if (ele.value == "") {
location.reload ();
}
},intervalT);
}
</script>
</head>
<body>
<input type="text" id="myBox" />
</body>
</html>

However, if they type something in and then delete it, the page will not reload. If you want it to, I can do that, but you didn't say it needed to.

Stephen

l_kris06
12-31-2008, 06:25 PM
Thanks for that, its what i want, while at it a little more tweaking would be great if it aint too much to ask.:)

1. A timer
2. Adding a value and then deleting stops timer , like you have mentioned.

Best,
Kris A

???
12-31-2008, 06:45 PM
The timer, do you want it to count down? Or what? As for the delete thing. Do you want it to restart if the time is up and then they delete everything? Like this?


<html>
<head>
<script type="text/javascript">
//Change these
var intervalT = 1000 //Time in milliseconds untill refresh
var elementId = "myBox" //ID of text box
//Don't change this crap
var ele;
var timeup = false;
var checkIt = function () {
if (timeup) {
if (ele.value == "") {
location.reload ();
}
}
}
window.onload = function () {
ele = document.getElementById (elementId);
setTimeout (function () {
timeup = true;
checkIt ();
},intervalT);
ele.onkeydown = function () {
setTimeout (checkIt,0);
}
}
</script>
</head>
<body>
<input type="text" id="myBox" />
</body>
</html>

So, using this new code. Say you type "hi", wait 5 seconds, then delete it. As soon as you delete the last character, it will reload because the 1 second timer is up.

Hope this helps,
Stephen

l_kris06
12-31-2008, 07:09 PM
allrite, i accept i wasnt really precise in what i wanted.:)

About the timer: A countdown timer would be ideal, and on timer completion, the page simply should refresh, that is if there is no data in the input box.
if a value is present in the inputbox, the timer must be frozen so as to not confuse the user.

Your recent code is perfect for usage, adding a timer would be more of a value add. I appreciate it. Many thanks.

Best,
Kris A

???
12-31-2008, 07:13 PM
As in, some text that counts down the time? Or should it count up? Should it be in milliseconds or seconds?

???
12-31-2008, 08:08 PM
OK. Here's a script with a countdown timer, I don't know if it's what you wanted, and my other script's better, but here you go:


<html>
<head>
<script type="text/javascript">
//Change these
var intervalT = 5000; //Time in milliseconds untill refresh
var intervalR = 10; //Time in milliseconds untill timer update
//WARNING intervalR: A NUMBER TOO SMALL CAN CAUSE LAG, AND TEREFORE CAUSE THE TIMER TO GO SLOWER THAN REAL TIME
var displayF = .001; //Amount that the time in milliseconds should be multiplied by before displaying
var displayP = 3; //Number of decimal places to be displayed
var displayS = " seconds"; //String to be inserted after display
var displayId = "displ"; //ID of display element
var elementId = "myBox"; //ID of text box
//Don't change this crap
var disp;
var ele;
var go = true;
var time = intervalT;
var inter = intervalT;
var toDecimalP = function (num) {
var fac = Math.pow (10,displayP);
var temp = Math.round (fac * num);
return temp / fac;
};
window.onload = function () {
disp = document.getElementById (displayId);
ele = document.getElementById (elementId);
var inter = setInterval (function () {
if (go) {
time -= intervalR;
if (time <= 0) {
location.reload ();
}
}
if (disp.hasChildNodes ()) {
disp.removeChild (disp.firstChild);
}
disp.appendChild (document.createTextNode (toDecimalP (time * displayF) + displayS));

},intervalR);
ele.onkeydown = function () {
setTimeout (function () {
if (ele.value == "") {
go = true;
}
else {
go = false;
}
},0);
};
};
</script>
</head>
<body>
<input type="text" id="myBox" />
<div id="displ"></div>
</body>
</html>


Stephen

l_kris06
12-31-2008, 08:20 PM
Hi Stephen,

Thats great code right there. I like the timer tagged with the ms part. :)
Very nice indeed. This is wht i wanted. Many thanks.really appreciate it.

Best,
Kris A