Adding a TimeOut to Javascript function?

The code below is currently working fine but I need to use TimeOut instead of the current way of hitting the MySQL database several times until it send back TestID.

result1, result2 are presented by the test which should be completed before this .js file will be called.

My knowledge in Java is really minimal so let me know if you have any questions before you try to convert my code to include the TimeOut.

Basically all what I want is the script will keep hitting 123.php with a delay of 2 seconds between each hit until 123.php send back a Test ID.

123.php only job is to echo the Test ID (EX: 3445) based on the data I presented to it.


Thank you so much for your help.

function test_completed(result1, result2, latency, server_id) {
        var myid = 0; 
        var i=0;
        do
          {
          myid = getTestID(result1, result2);
                i++;
                if (myid > 15) i = 15;
          }
        while (i<=14);

        if(myid!=-1) {
        document.getElementById("mypic").innerHTML = "<img src='http://www.mywebsite.com/result/" + myid + ".png'><br>" + "Direct Link:<br><input type='text' onClick='highlight(this);' readonly='readonly' style='width: 246px' value='http://www.mywebsite.com/result/" + myid + ".png'<br>" + "Forum Link:<br><input type='text' onClick='highlight(this);' readonly='readonly' style='width: 246px' value='[URL=http://www.mywebsite.com][IMG]http://www.mywebsite.com/result/" + myid + ".png[/IMG][/URL]'<br>"; + pageTracker._trackPageview('/done/');
        }
}


function getTestID(result1, result2) {
                var xmlhttp = null;
                if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
                if(xmlhttp != null) {
                        url="123.php?r1=" + result1 + "&r2=" + result2 + "&cachebuster=" + new Date().getTime();
                        xmlhttp.open("GET",url,false);
                        xmlhttp.send();
                        return xmlhttp.responseText;
                }
                return -1;
}


function highlight(field) {
field.focus();
field.select();
}

Open in new window

Dido123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StealthyDevCommented:
This would help you

<script>

        var timerID = null;
        var timerRunning = false;
        var delay = 2 * 60 * 1000; // Time interval mins * sec * mili-sec (change mins only)

        function StopTheClock()
        {
			if(timerRunning)
			{
					clearTimeout(timerID);
			}
			timerRunning = false;
        }

        function StartTheTimer()
        {
			timerRunning = true;
			timerID = setTimeout("AjaxTimeOut()", delay);
        }

        function AjaxTimeOut()
        {
                StopTheClock();
                
				//put your code here

                StartTheTimer();
        }

		StartTheTimer();

</script>

Open in new window

0
Dido123Author Commented:
@senthurpandian .. As I posted in my question, My java knowledge is really minimal. Could you please apply your code to my main code. I mean put it all together, just to make sure that I wouldn't miss anything up. Please.

Thank you so much for the help.
0
StealthyDevCommented:
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Dido123Author Commented:
Yes, but the other one is totally missed up and no working answers were presented.
0
StealthyDevCommented:
Okay Dido123, sorry to mess up your code.

Do you really like my reply or need anyother expert?
Anyways, i have tried to help you again!

Find the codes attached. Test the same code and do revert back.

Best Regards.



<script>
var timerID = null;
var timerRunning = false;
var delay = 10 * 1000; // Time interval 10 * = sec

function StopTheClock()
{
        if(timerRunning)
        {
                clearTimeout(timerID);
        }
        timerRunning = false;
}

function StartTheTimer()
{
        timerRunning = true;
        timerID = setTimeout("TimeOut()", delay);
}

var myid = 0; 

function TimeOut()
{
		myid = getTestID(g_result1, g_result2);
        if(myid > 0)
        // Add your logic here, whatever you get from getTestID's negative (might be -1 or "" or 0)
        {
                StopTheClock();
                document.getElementById("mypic").innerHTML = "<img src='http://www.mywebsite.com/result/" + myid + ".png'><br>" + "Direct Link:<br><input type='text' onClick='highlight(this);' readonly='readonly' style='width: 246px' value='http://www.mywebsite.com/result/" + myid + ".png'<br>" + "Forum Link:<br><input type='text' onClick='highlight(this);' readonly='readonly' style='width: 246px' value='[URL=http://www.mywebsite.com][IMG]http://www.mywebsite.com/result/" + myid + ".png[/IMG][/URL]'<br>"; + pageTracker._trackPageview('/done/');
				alert(myid);
        }
        else
        {
                StartTheTimer();
        }
}

var g_result1, g_result2; // Globals

function test_completed(result1, result2) {
        g_result1 = result1;
        g_result2 = result2;
        TimeOut();
}

function getTestID(result1, result2) {
	var xmlhttp = null;
	if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
	if(xmlhttp != null) {
		//url="ajax-timeout.html";
		url="123.php?r1=" + result1 + "&r2=" + result2 + "&cachebuster=" + new Date().getTime();
		xmlhttp.open("GET",url,false);
		xmlhttp.send();
		return xmlhttp.responseText;
	}
	return -1;
}

function highlight(field) {
field.focus();
field.select();
}

test_completed(1,2);

</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PorpathamCommented:
Hi,
Just check is it useful.

<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000);
}
</script>
</head>

<body>
<form>
<input type="button" value="Display timed alertbox!"
onClick="timedMsg()" />
</form>
</body>
</html>
0
StealthyDevCommented:
Hi Dido123, did you check my previous script?

I have tested with a dummy ajax page, it worked. Do revert!
0
Dido123Author Commented:
senthurpandian, You F***en did it :D

I copied your last code above without editing anything and it's working like a charm! :D :D :D

Thank you genius man :)

I'll close both questions now but just three more "small" things.

1st) What is test_completed(1,2); as the end of my code.
2nd) What was the problem in your last "not working" code.
3rd) I want to execute pageTracker._trackPageview('/done/'); which you can find at the end of ("mypic").innerHTML once the test completed. I mean it doesn't have to wait until the script find myid then post mypic to execute it.


Thank you sooooooooo much .. God Bless You! :)
0
StealthyDevCommented:
Hi again! Glad to know that it worked!

1. I have used test_completed(1,2); to test the code with a dummy AJAX file. (you can remove!)

2. I have mentioned that you need to change your logic in the if statement:
(// Add your logic here, whatever you get from getTestID's negative (might be -1 or "" or 0))
It is changed from if(myid < 0) to if(myid > 0)

3. Do you want to show the [ (a) Image (b) only page tracker ] as soon as the test is completed ie., even before getting myID?


Revert for point#3.
Regards.

0
Dido123Author Commented:
I want to show the page tracker only as as the test is completed even before getting myid.

Thanks again!
0
Dido123Author Commented:
Thank you Genius! :)
0
StealthyDevCommented:
Okay, i understand.

Please change your test_completed function only, as attached.

function test_completed(result1, result2) {
        g_result1 = result1;
        g_result2 = result2;
		document.getElementById("mypic").innerHTML = pageTracker._trackPageview('/done/');
        TimeOut();
}

Open in new window

0
Dido123Author Commented:
It gives back "undefined" when the test is completed. Which means it didn't work.
0
Dido123Author Commented:
Update, I see it reported correctly in my Google Analytics but it gives "undefined" msg on my page where the "mypic" div should be.
0
StealthyDevCommented:
May be i think you need to wait until the myid is rendered?
0
Dido123Author Commented:
What about assign it to another id? like this :

document.getElementById("mypic2").innerHTML = pageTracker._trackPageview('/done/');
0
StealthyDevCommented:
Yeah you can try it. But the problem is, i am not sure what is:

pageTracker._trackPageview('/done/');

When you will get it update? what is the value inside it?

0
Dido123Author Commented:
This is what id does : http://www.google.com/support/googleanalytics/bin/answer.py?hl=en&answer=55520

It's a line presented by Google Analytics to report events.
0
StealthyDevCommented:
Yes, then, you can obviously use it.

If you will get the correct output for that line, go ahead. Any help required?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.