函数的 JS 顺序调用失败


JS sequential calling of function fails

我需要依次执行一个JS函数。 我可以单独执行这些函数并且它们有效,但是当我将它们按顺序放置时,我只得到 openPatient() 和 openMessage() 不会执行。 我的 JS 函数

function openPatient() {
 myRestoreSession();
 opener.top.RTop.document.location.href = '../patient_file/summary/demographics.php?set_pid=<?php echo attr($ptid); ?>';
}
function openMessage(messageid) {
 myRestoreSession();
 document.location.href = 'upload_form.php?messageid=' + messageid;
} 

我的函数调用:

echo " onclick='"openPatient().then(openRequest(" .
     "'" . addslashes($postid)      . "'," .
     "'" . addslashes($v1[1]['type']) . "'"  .
     "))'">" . text($v1[1]['datetime']) . "</td>'n";

函数调用存在于此进程中:

<?php
// Generate a table row for each pending portal request or message.
// This logic merges requests with messages by date.
$v1 = each($result['list']);
$v2 = each($result['messages']);
while ($v1 || $v2) {
  echo " <tr class='detail' bgcolor='#ddddff'>'n";
  if (!$v2 || $v1 && $v1[1]['datetime'] < $v2[1]['datetime']) {
    $postid = $v1[1]['postid'];
    $ptname = patientNameFromLogin($v1[1]['user']);
    // Get the portal request data.
    if (!$postid) die(xlt('Request ID is missing!'));
    $result2 = cms_portal_call(array('action' => 'getpost', 'postid' => $postid));
    if ($result2['errmsg']) {
      die(text($result2['errmsg']));
    }
    // Look up the patient in OpenEMR.
    $ptid = lookup_openemr_patient($result2['post']['user']);
    echo "  <td>" . text($v1[1]['user']) . "</td>'n";
    echo "  <td style='cursor:pointer;color:blue;' onclick='"openPatient()'">" .text($ptname       ) . "</td>'n";
    echo "  <td style='cursor:pointer;color:blue;'";
    echo " onclick='"openPatient().then(openRequest(" .
         "'" . addslashes($postid)      . "'," .
         "'" . addslashes($v1[1]['type']) . "'"  .
         "))'">" . text($v1[1]['datetime']) . "</td>'n";
    echo "  <td>" . text($v1[1]['type'    ]) . "</td>'n";
    echo "  <td align='center'><input type='checkbox' name='form_req_cb[" .
         attr($postid) . "]' value='" . attr($postid) . "' /></td>'n";
    $v1 = each($result['list']);
  }
  else {
    $messageid = $v2[1]['messageid'];
    $ptname = patientNameFromLogin($v2[1]['user']);
    echo "  <td>" . text($v2[1]['user']) . "</td>'n";
    echo "  <td>" . text($ptname       ) . "</td>'n";
    echo "  <td style='cursor:pointer;color:blue;'";
    echo " onclick='"openMessage(" .
         "'" . addslashes($messageid)      . "'" .
         ")'">" . text($v2[1]['datetime']) . "</td>'n";
    echo "  <td>" . text($v2[1]['user'] == $v2[1]['fromuser'] ?
         xl('Message from patient') : xl('Message to patient')) . "</td>'n";
    echo "  <td align='center'><input type='checkbox' name='form_msg_cb[" .
         attr($messageid) . "]' value='" . attr($messageid) . "' /></td>'n";
    $v2 = each($result['messages']);
  }
  echo " </tr>'n";
}
?>

我认为部分问题可能是 openPatient() 在另一个窗口中打开。 也许它正在失去焦点。 任何解决此问题的提示将不胜感激。

编辑:

我尝试并提供帮助的是将return this;添加到 openPatient():

function openPatient() {
 myRestoreSession();
 opener.top.RTop.document.location.href = '../patient_file/summary/demographics.php?set_pid=<?php echo attr($ptid); ?>';
 return this;
}
然后执行下一个函数

,但下一个函数执行得太快。 它需要等待 openPatient() 完全加载,然后再执行 openMessage()。 我尝试添加setTimeout( wait, 1000 );但随后 openMessage() 根本没有执行。

解决方案:

电话:

    echo "  <td style='cursor:pointer;color:blue;'";
    echo " onclick='"openPatient();setTimeout(function(){openRequest(" .
         "'" . addslashes($postid)      . "'," .
         "'" . addslashes($v1[1]['type']) . "'"  .
    ")}, 2500);'">" . text($v1[1]['datetime']) . "</td>'n";

功能:

function openPatient() {
 myRestoreSession();
 opener.top.RTop.document.location.href = '../patient_file/summary/demographics.php?set_pid=<?php echo attr($ptid); ?>';
 return this;
}
function openMessage(messageid) {
 myRestoreSession();
 document.location.href = 'upload_form.php?messageid=' + messageid;
} 

成功的关键:return this;和匿名函数的使用,setTimeout在调用中。

有帮助的帖子:"return this"在javascript函数中做什么?设置超时延迟不起作用