Click to See Complete Forum and Search --> : Is there no way to halt code execution?


semi-sentient
08-18-2006, 02:34 PM
I have a situation where I need to halt code execution and wait for feedback (a button click) from the user. I've written a custom dialog class that is designed to replace both alert() and prompt(), but the issue that I'm having is that I'm unable to stop execution when the dialog (basically a div that overlays the page) appears.

Here is some sample code that shows my dialogs usage:

myDialog.show(); // shows the dialog

/* code execution needs to halt at this point */
/* this will NOT work: while(myDialog.getButtonClicked() == "") {} */

// determine which button was clicked and take appropriate action
if (myDialog.getButtonClicked() == "yes") {
// do something
} else {
return;
}


If I write a loop that waits for the button click, browser resources are consumed and no interaction with the dialog is possible, not to mention the browser will give you a message that allows you to stop code execution alltogether.

When using the built-in alert() and prompt() methods, code execution halts until a button is clicked.

This is the same functionality I'm looking for. I'm almost positive that this is not really possible, but I thought I'd ask anyway. It would be fantastic if there was a way to mimic multi-threading in JavaScript, this way I can just loop until I have an appropriate button click, but this seems impossible as well.

Any ideas?

cybercampbell
08-18-2006, 02:47 PM
I think I saw somthing like this on a site the other day.

The URL is:www.podooch.com (http://www.podooch.com)

I could be wrong so don't hold it against me....but there definitely was some sort of custom popup alert thing.

Hope this helps.

Cybercampbell

semi-sentient
08-18-2006, 03:49 PM
^^^ While I didn't see any alert, clicking login does bring up a login "prompt". He's doing something similar, but more than likely using Ajax to trigger actions. I'm not using any Ajax in mine, and it wouldn't really help if I did because I need to be able to do be able to stop execution before doing any conditional logic.

felgall
08-18-2006, 03:52 PM
Is http://javascript.about.com/library/blmodald1.htm what you are looking for?

semi-sentient
08-18-2006, 04:12 PM
Is http://javascript.about.com/library/blmodald1.htm what you are looking for?

Not really. What the above is doing is calling the dialog when a link or button is clicked, then calling a function to handle processing when the result is returned. If it were calling the dialog within a function and wanted to process the results in the same function, that technique would fail because the function would continue to execute regardless of if the user clicked "OK" or "Cancel".

In my case, the dialog can be called throughout several places in a very large function, and processing must not continue until a result is received so the next condition can be tested. It's not so much a problem with how I'm doing things in the dialog class as opposed to some of our *ahem* poorly designed JS, but if there is a way to halt execution then I could get around this problem.

In a simple environment where complex functions are not used, my dialog class works great, but to have it work anyway regardless of complexity I need to be able to halt execution the same way that alert(), confirm(), and prompt() halt execution.

Kor
08-18-2006, 04:48 PM
a confirm is what you need?:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function blah(){
if(confirm('Wanna continue? Click OK!')){
alert('you\'ve chosen to continue');
}
}
</script>
</head>
<body>
<span onclick="blah()">click here</span>
</body>
</html>

semi-sentient
08-18-2006, 06:10 PM
^^^ No.

I've written my own class (object) that has multiple dialog types which is meant to replace the built-in alert(), confirm(), and prompt() methods/objects.

I need to halt code execution similar to how execution is halted when those native dialogs are called.

felgall
08-18-2006, 07:31 PM
The only way to halt execution in Javascript is to split it up into separate functions the way I do in my example script. Javascript does not have a way to stop at a particular statement and wait for a response apart from alert() confirm() and prompt().