JS顺序调用函数失败

时间:2016-02-11 16:12:41

标签: javascript php

我需要按顺序逐个扩展一个JS函数。我可以单独说明这些函数并且它们可以工作但是当我按顺序放置它时我只得到openPatient()并且openMessage()不会exicute。我的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;
}

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

1 个答案:

答案 0 :(得分:0)

解决方案:

电话:

    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

帮助的帖子: What does "return this" do within a javascript function? setTimeout delay not working