This topic is locked
[SOLVED]

 fire javascript calculation automatically or at page load

11/16/2020 11:54:05 PM
PHPRunner General questions
woodey2002 author

Hi Guys,
I have a nice little piece of JS that calculates how many days a patient has been in hospital for.
I takes a date admitted and compares it to today's date to calculate how many days a patient is in hospital.
It fires really well with the admission date field on('change') event.
The problem I'm having is the day calculation is only updated if the user changes the actual date of admission.
I really would love to have a dynamic day count calculation updated each time a user loads the page without having to touch anything!
I have have some success in the past using setInterval but this time I can't get it to fire automatically.
Also tried the window.onload ( function(){ with no luck also
Any ideas would be very much appreciated.
Many thanks

J



var ctrDayDate1 = Runner.getControl(pageid, 'admission_Date');

var ctrDayRem1 = Runner.getControl(pageid, 'total_Days_Admitted');
setInterval("setDates1()", 300);


function setDates1() {

if (ctrDayDate1.getValue()) {

var ddate1 = new Date(ctrDayDate1.getValue());
ddate1.setDate(ddate1.getDate());

var out1 = (ddate1.getMonth() + 1) + "/" + ddate1.getDate() + "/" + ddate1.getFullYear();

//ctrDayExp.setValue(out);

var currDate1 = new Date();

var cDate1 = currDate1.getTime();

var eDate1 = ddate1.getTime();

ctrDayRem1.setValue(parseInt((cDate1 - eDate1) / 1000 / 60 / 60 / 24) + 1);

}

}

//});
HJB 11/17/2020
admin 11/17/2020

I'm not quite sure why would you need to use setInterval() here. If this code is added to Javascript OnLoad event you can simply call your setDates1() function right away.
In either case you need to troubleshoot your application using Chrome developer Tools to see if that function is not being executed or just doesn't calculate something properly. It is all explained in this video:

https://www.youtube.com/watch?v=o5o18eawWpE

woodey2002 author 11/17/2020

Many thanks to you both.
Sorted now after following your kind advice.
Thanks again!
J