Jump to content

Important : Wait function


harshpandya
 Share

Recommended Posts

Hi guys :I have this function i am writing, in which i need to include a wait function that waits for certain amount of time that i declare and than and than it should proceed further. How should i do that?Here is my code:function dothis(){bla bla....wait(30); //this wait function should wait for 30 seconds and if it gets response it should let program go further. //if it does not then it should not let the further code to process. //Its like i am waiting for 30 seconds for response and doing nothing else. //further code...dosomething();bla.. bla..bla...}Is there a way to wait like this - i tried to built my custom function but it does not do what i want. Please Help:Thanks,

Link to comment
Share on other sites

Thanks but if you did not notice - what is happening right now in my code:the code after wait function is being executed - same thing with setTimeout - when i call setTimeout it runs that perticular function after certain time but the rest of the code keeps running.What i want is - when i call wait function the whole code should just freeze untill it gets the permission. I m sorry i want freeze function not setTimeout.Please help

Link to comment
Share on other sites

There is no freeze function per se. A true freeze would hijack your client's browser and possibly the whole computer till it was done. No mouseclicks, no nothing. That's why the timeout and interval functions exist. Isn't there a way that you can put "the rest of the code" into the function called by setTimeout? Or into a separate function that is itself called by the timeout function?It's probably your only option.Maybe if you explained more about what you're up to and why?

Link to comment
Share on other sites

It's entirely possible that I'm wrong, but from what i can gather it sounds like ur doing an AJAX request and you want to use it in the syncronis mode. If thats true, then all you have to do is this:

var req = new XMLHttpRequest();//Screw IE;preq.open('yourPage.html','GET',false);// The arguments are page, method, async. Async can be true or false, if false, it loads syncronisly(sp?) and your code will be processed //in the usual line-by-line manner.

If you're doing something else, then It's as Deidre's Dad said, there's really no sleep function that I am aware of in JS.

Link to comment
Share on other sites

it sounds like ur doing an AJAX request and you want to use it in the syncronis mode.
I wondered about that too. The first time I used AJAX was for a web-app where the user clicked a button to save some spreadsheet data to the server. Naturally, being able to do that asynchronously was a blessing. Making the page even more app-like, I figured I'd better give my user a chance to save unsaved data on a window.unload. I already had one save function, so why write another? Only I couldn't figure out why sometimes the data got saved and sometimes it got lost. When I figured it out, I decided to throw up a confirm dialog just as a way to make the page stay open while the data got sent. But that was just stupid. Eventually, I figured out that I could use the same function synchronously. That made sure POST went off before the page unloaded. Bye-bye dialog.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...