简体   繁体   English

如何暂停和恢复javascript?

[英]How to pause and resume javascript?

I want to pause the javascript code below after it calls $('#dialog').dialog('open'); 我想在调用$('#dialog')之后暂停下面的javascript代码。dialog('open'); and until user responds this dialog box. 直到用户响应此对话框。 I used while loop but it stops all the javascript. 我使用了while循环,但它停止了所有的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 我只想暂停javascript,直到用户输入一些值,如提示

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_comment',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. 如果你想在等待用户的响应时“暂停”javascript的执行,那么你的方法是错误的。

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. 然后,当用户填写对话框上的信息并提交它时,让它调用另一个javascript操作来处理该数据。

Some psuedo code below: 一些伪代码如下:

Page loads: 页面加载:

$(".DialogBox").show();

When the DialogBox OK button is clicked 单击DialogBox OK按钮时

$(".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"); 警报(“按OK继续”); ?

You're talking about a modal jQuery dialog , which must be dealt with before the user can proceed with any other GUI elements. 你在谈论一个模态 jQuery对话框 ,必须在用户继续使用任何其他GUI元素之前处理它。

$('#dialog').dialog("option", "modal", true);
// ^ it would be best to set this when you
// _create_ the dialog, but this works too

$('#dialog').dialog('open');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM