Forum Moderators: open

Message Too Old, No Replies

Hi guys, my javascript breaks when it loops and things screw up

Need some javascript enlightenment

         

haryanto

11:31 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



Hi guys,

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>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>

orion_rus

7:49 pm on Feb 20, 2005 (gmt 0)

10+ Year Member



I think it's very hard to compiler to make all this may be u should try setInterval(taketime,500) i think it's much better way if loopit contain all what is there now.
Good luck to you

haryanto

8:24 pm on Feb 20, 2005 (gmt 0)

10+ Year Member



I can't sleep at night thinking about this problem.

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();

Bernard Marx

9:29 pm on Feb 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure what the script is doing really, but the it appears to be attempting to make synchronous HTTP requests every half-second. That's bound to gum up the works.

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.

haryanto

10:03 pm on Feb 20, 2005 (gmt 0)

10+ Year Member



I am using xmlhttp to communicate with an external script every x seconds.

XML is a new thing and I think not a lot of javascript programmers are aware its existence.

Sometimes the external script doesnt respond quickly and my loop breaks for some reason.

Bernard Marx

10:15 pm on Feb 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know :) Read my last comment again.
Try asynchronous requests.

orion_rus

10:16 pm on Feb 20, 2005 (gmt 0)

10+ Year Member



hmm) but browser's understand xhtml not much have good own xml translater, you should use xslt then to make xhtml, but javascript is fully compatable with xhtml. And it should work well then. May be u can describe ur problem and we can present you another decision?