Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 25th, 2005, 01:07 PM
Registered User
 
Join Date: Aug 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with setTimeout and function returns

Hi all

Does anyone know of a sensible way of making setTimeout or setInterval (or any similar function) pause the execution of the calling script until they return?

For compatibility with a legacy system, I need to make a function call return a value from a server-side script.

To accomplish this, I’ve presently got the script loading the request data into a form in a child frame, which is submitted to the server-side script for processing. The server then returns a new form containing the results of the processing (so it’s not instantly available to the script – I have to handle the delay and only return when the data has appeared).

The problem here is that the one part of this design that’s absolutely critical is that the data comes back in a function call. If I wanted it to come back in an alert() or document.write(), I could (and have) done it. You make the return function call itself within a timeout and, when it finds data, output it.

When I try this within returning the data as a function return, the basic problem seems to be that both setTimeout and setInterval mark the code to run in the future and then keep going. This means that the code runs the first time, finds the script hasn’t yet returned so sets itself to run again in the future and then keeps going. Net result it runs once, finds it’s not there and returns an error saying it can’t find the data. Whereas what I’d much rather it did would be pause execution of the script until the timeout had been met.

I’m aware I could theoretically do this by executing a do-nothing loop that sat there until the function returned – but that would be a real processor hog.

I’m also aware I could do this as AJAX – but I’m not over-familiar with it and neither are others who’d have to maintain this script, so I’m trying to avoid that for simplicity at this stage.

Does anyone know a (non-CPU hogging) way of making either setTimeout or setInterval actually stop execution of whatever has called them until their condition has been met? Or an alternative means of polling an external variable on a schedule until it contains a specified item or a timeout is reached?

Thanks,

Greg

 
Old August 26th, 2005, 01:23 AM
Friend of Wrox
 
Join Date: Oct 2004
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919
Default

Greg,

  make a flag is false ,and make it true only when user enters some value (i.e as per ur requirement) then pause the function by using setinterval,settimeout.
hope this will help you.

Cheers :)

vinod
 
Old August 26th, 2005, 03:15 AM
Registered User
 
Join Date: Aug 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not sure that'll work - it's a server-side script setting the value not the user, plus research is so far showing that setTimeout _doesn't_ pause the script, it just sets the line to run later in a different context.

If you want to see what I mean, try this:

setInterval("alert('Hello')", 2000);
alert("world");

Sorry


Greg

 
Old August 27th, 2005, 05:32 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How about this Greg:

function pauseScript(mSecs){

        theDate = new Date();
        var todaysDate = null;

        do {

        var todaysDate = new Date();
            }
            while (todaysDate-theDate < mSecs);

            }

Call this routine where mSecs is the number of milliseconds you want the script to pause.

HTH
Joe
 
Old August 30th, 2005, 03:07 AM
Registered User
 
Join Date: Aug 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Surely that hits the problem I mentioned earlier that it's a do nothing loop that'll go round very quickly doing no work and so prove a real CPU hog?

 
Old August 31st, 2005, 02:00 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need to modify the code so that immediately after the call to the server-side function you call setTimeout on a helper function. The helper function checks whether the server-side call has returned a value, if not it calls itself, again with setTimeout. If it has returned it calls another function which continues with your main code.
Code:
var sResult = null;
function callServerSideFunction()
{
  sResult = callCodeOnServer();
  window.setTimeout(waitAndSee, 500);
}

function waitAndSee()
{
  if (sResult)
  {
    proceedWithCode(sResult);
  }
  else
  {
  window.setTimeout(waitAndSee, 500);
  }
}

function proceedWithCode(Result)
{
  //
}

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
getting a return value from setTimeout() peirix Javascript 9 November 28th, 2008 06:22 PM
function returns Incorrect syntax keyvanjan ASP.NET 2.0 Professional 1 July 29th, 2007 10:36 AM
Count Function returns strange value tslag XSLT 2 June 23rd, 2006 10:13 AM
active directory: problem with 1000 returns empty C# 2005 0 November 18th, 2005 04:39 AM
Using setTimeout PortGuy Javascript 7 March 25th, 2004 02:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.