How to pause and resume javascript?
I want to pause the javascript code below after it calls $('#dialog').dialog('open'); and until user responds this dialog box. I used while loop but it stops all the javascript. I just want to pause this function. Any ideas?
Note: I dont want to split my code into two functions. I just want to pause javascript until the user enters some values, like a prompt
function sendSubComment(button){
form = button.parentNode
nodes= form.elements;
cmt = $(nodes.item(0)).val();
cmt = cmt.replace(/\n/g,"<br>");
title = $(nodes.item(1)).val();
for(i=0;i < nodes.length;i++){
nodes.item(i).disabled = 'true';
}
c = form.parentNode.parentNode;
if(checkUserLogin() == false){
$('#dialog').dialog('open');
//pause starts here
//return the values of user
//resume javascript
}
document.title = c.id;
$.post('index/phplibrary/action.php',{cmd:'insert_c开发者_开发百科omment',cmt:cmt,title:title,id_topic:<?php echo $GLOBALS['id_topic']; ?>,id_parentComment:c.id,subcomment:'1'},function(result){
$.post('ajax/comments.php?cmd=get_subcomment&id_subcomment='+result,function(res){
extendMakeComment(form);
node = c.childNodes[3];
document.title = node.className;
t = document.createElement("DIV");
t.innerHTML = res;
t = t.childNodes[0];
$(t).hide();
node.appendChild(t);
$(t).show("slow");
form.reset();
for(i=0;i < nodes.length;i++){
nodes.item(i).disabled = '';
}
});
});
}
this code is how i create dialog box
$('#dialog').dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function() {
$(this).dialog("close");
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
Your going about it the wrong way if you want to "pause" the execution of the javascript while you wait for a response from the user.
You should open the dialog box when the user requests it (Clicks a button, or on page load, etc). Then when the user fills out the info on the dialog box and submits it, have it call another javascript action to process that data.
Some psuedo code below:
Page loads:
$(".DialogBox").show();
When the DialogBox OK button is clicked
$(".DialogBox input[type=submit]").click(function(){
// Process the data
});
Instead of pausing, just contain the rest of the code in a function that you call when the dialog information is submitted:
if(checkUserLogin() == false){
$('#dialog').dialog('open');
$('#dialog').find('.classOfSubmitButton').click(function(e){
loginCode();
}
} else {
loginCode();
}
function loginCode() {
// everything after resume javascript comment
}
Using events are probably the way to go.
function sendSubComment(button){
if(checkUserLogin() == false){
$( "#dialog" ).bind( "dialogclose", function(event, ui) {
if(checkUserLogin() == true) //this check is needed to eliminate dead loop
sendSubComment(button); //call again
$( "#dialog" ).unbind(event, ui);
}).dialog('open');
return; //not going further
}
//do the other stuffs
}
alert ("Press OK to continue"); ?
You're talking about a modal jQuery dialog, which must be dealt with before the user can proceed with any other GUI elements.
$('#dialog').dialog("option", "modal", true);
// ^ it would be best to set this when you
// _create_ the dialog, but this works too
$('#dialog').dialog('open');
精彩评论