Forum Moderators: open
I have a javascript loop , loopit() , that loops every 0.5 seconds. During the loop, it executes the function takestime(). Now, the problem is that the function takestime() takes a little too long sometimes and the loop breaks for some reason.
I was wondering if we can keep the loop alive even if the takestime() function is taking way too long to execute or if there is any way to revive the loop when things screw up.
Here is the generic script:
<script language="JavaScript" type="text/JavaScript">
function takestime(e) {
//some dummy function that takes time
}
function loopit() {
takestime(e);
alert('still looping');
setTimeout('loopit()',500);
}
loopit();
</script>
Below is the real script, its VERY MESSY and hard to understand so it might be better to use the generic script albeit its too simplified.
<script type="text/javascript">
function stopError() {
return true;
}
window.onerror = stopError;
</script>
<script type="text/javascript">
var xmlhttp,alerted
if (!xmlhttp &&!alerted) {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
alert("You need a browser which supports an XMLHttpRequest Object.\nMozilla build 0.9.5 has this Object and IE5 and above, others may do, I don't know, any info jim@jibbering.com")
}
}
function go() {
if (xmlhttp) {
d=document
xmlhttp.open("GET", "getit.txt",true);
xmlhttp.send(null)
}
}
</script>
<script language="JavaScript" type="text/JavaScript">
function winClose() {
// alert(window.pageYOffset);
go();
document.getElementById("the_y").innerHTML = window.pageYOffset;
document.getElementById("the_x").innerHTML = window.pageXOffset;
setTimeout('winClose()',500);
}
</script>
<script>
if (!document.layers)
document.write('<div id="divStayTopLeft" style="position:absolute">')
</script>
<layer id="divStayTopLeft">
FROM LEFT <div id="the_x" style="width: 100; height: 20;"></div>
FROM TOP <div id="the_y" style="width: 100; height: 20;"></div>
</layer>
<p>
<script type="text/javascript">
var verticalpos="frombottom"
if (!document.layers)
document.write('</div>')
function JSFX_FloatTopDiv()
{
var startX = 500,
startY = 150;
var ns = (navigator.appName.indexOf("Netscape")!= -1);
var d = document;
function ml(id)
{
var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
if(d.layers)el.style=el;
el.sP=function(x,y){this.style.left=x;this.style.top=y;};
el.x = startX;
if (verticalpos=="fromtop")
el.y = startY;
else{
el.y = ns? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
el.y -= startY;
}
return el;
}
window.stayTopLeft=function()
{
if (verticalpos=="fromtop"){
var pY = ns? pageYOffset : document.body.scrollTop;
ftlObj.y += (pY + startY - ftlObj.y)/8;
}
else{
var pY = ns? pageYOffset + innerHeight : document.body.scrollTop + document.body.clientHeight;
ftlObj.y += (pY - startY - ftlObj.y)/8;
}
ftlObj.sP(ftlObj.x, ftlObj.y);
setTimeout("stayTopLeft()", 50);
}
ftlObj = ml("divStayTopLeft");
stayTopLeft();
}
JSFX_FloatTopDiv();
</script>
<body onLoad="winClose()">
<table width="1000" height="5000" border="1">
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
I was thinking if I have another checker loop to make sure the main loop is not broken. If it is, then restart it. Easier said than done. My JS skill is not as advanced as some of the gurus on this forums.
e.g.
function loopit()
{
takestime();
settimeout(loopit(),500);
}
function check_loopit()
{
//SOMETHING TO CHECK THE LOOP STATUS
settimeout(check_loopit(),2000);
}
loopit();
check_loopit();
Try using asynchronous requests, and put the setTimeout into the callBack function.
Javascript isn't truly multithreaded. That is to say, no 2 threads appear to be able to run at once.