0

I need to exicute one JS function after another sequencually. I can exicute these functions individually and they work but when I put them in a sequence I only get the openPatient() and the openMessage() does not exicute. My JS functions

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;
} 

My function call:

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

This function call exists in this process:

<?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";
}
?>

I am thinking part of the problem may be that openPatient() opens in another window. Perhaps it is loosing focus. Any tips to fix this would be appreciated.

EDIT:

What I have tried and helps is adding return this; to openPatient():

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

This then executes the next function but the next function executes too soon. it needs to wait for openPatient() to fully load before executing openMessage(). I have tried adding setTimeout( wait, 1000 ); but then openMessage() does not execute at all.

Craig Tucker
  • 1,051
  • 1
  • 11
  • 27

1 Answers1

0

The solution:

The call:

    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";

The functions:

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;
} 

Keys to success: return this; and the use of the anonymous function with setTimeout in the call.

Posts that helped: What does "return this" do within a javascript function? setTimeout delay not working

Community
  • 1
  • 1
Craig Tucker
  • 1,051
  • 1
  • 11
  • 27