Client side method calculation question

Hello, 

I am wondering if anyone could help me solve this issue:

I am trying to have a calculation on the client side figure out how much time is between now (current time) and a due date set on the page.

1: I am grabbing the date from the dueDate field on the page and setting it to a variable 

2: Getting the current date and time and setting that to a variable

3 & 4: Initializing int (Number) variables via meta programming standards (not critical step)

5: set a while loop, checking if boolean/checkbox is checked off.

6: Subtracting values to find the difference with Math.abs();

7: defining the variable even further with Math.ceil();

8: Setting the value to a field on the page, with "this.set" 

This was working before I added the while() condition, am I going wrong somewhere?

I have already been trying to figure this out for 3 hours so hopefully someone can see my mistake and teach me something new, haha. 

 

Thanks, Community!

 

File attachments
Like 0

Like

3 comments

I would do something like this

define("ActivityPageV2", [], function() {
	return {
		entitySchemaName: "Activity",
		details: /**SCHEMA_DETAILS*/{}/**SCHEMA_DETAILS*/,
		modules: /**SCHEMA_MODULES*/{}/**SCHEMA_MODULES*/,
		methods: {
			onDueDateChanged: function() {
				var now = new Date();
				var dueDate = this.get("DueDate");
				var diffHrs = this.getDateDiff(now, dueDate, "hours");
				this.set("KwlString1", diffHrs.toString());
			},
			getDateDiff: function(firstDate, secondDate, identifier) {
				var diffMs = (secondDate - firstDate); // milliseconds between firstDate & secondDate
				var diffDays = Math.floor(diffMs / 1000 / 60 / 60 / 24);
				var diffHrs = Math.floor(diffMs / 1000 / 60 / 60);
				var diffMins = Math.floor(diffMs / 1000 / 60);
				switch (identifier) {
					case "milliseconds":
						return diffMs;
					case "minutes":
						return diffMins;
					case "hours":
						return diffHrs;
					case "days":
						return diffDays;
					default:
						return diffMs;
				}
			}
		},
		attributes: {
			"OnDueDateChangeAttribute": {
				dependencies: [
					{
						columns: ["DueDate"],
						methodName: "onDueDateChanged"
					}
				]
			}
		},
		rules: {},
		businessRules: /**SCHEMA_BUSINESS_RULES*/{}/**SCHEMA_BUSINESS_RULES*/,
		diff: /**SCHEMA_DIFF*/[
			{
				"operation": "insert",
				"name": "STRING8bd032e4-6e0f-4e1c-afdb-2ecf1041382b",
				"values": {
					"layout": {
						"colSpan": 12,
						"rowSpan": 1,
						"column": 0,
						"row": 9,
						"layoutName": "Header"
					},
					"bindTo": "KwlString1",
					"enabled": true
				},
				"parentName": "Header",
				"propertyName": "items",
				"index": 17
			}
		]/**SCHEMA_DIFF*/
	};
});

 

Eugene Podkovka,

Thank you for your quick response. It works well, but I was wondering how you would modify the method to: 

  1. Only set the "diffHours" once the page is initialized.
  2. Go into a while loop checking "while a checkbox is unchecked - run loop" that would set the hours/minutes/seconds remaining every second (or n time to create a "live" feel to the process).
  3. Once checkbox is checked, stop entire process and save current time. 

I added the a while loop to check if the boolean/checkbox is false (0) and it seems to be working.

As for getting this while loop to run after initialization, this is where I am having troubles, and after going through bpm'online's and the Mozilla Foundation's Documentation I turn to you, Eugene.

methods: {
	onDueDateChanged: function() {
		var now = new Date();
		var completeTask = this.get("UsrCompleteTask");
		var dueDate = this.get("DueDate");
		var diffHrs = this.getDateDiff(now, dueDate, "minutes");
        while(completeTask == 0) {
			this.set("UsrSTRING1", diffHrs.toString());
		}
	},
	getDateDiff: function(firstDate, secondDate, identifier) {
		var diffMs = (secondDate - firstDate); // milliseconds between firstDate & secondDate
			var diffDays = Math.floor(diffMs / 1000 / 60 / 60 / 24);
			var diffHrs = Math.floor(diffMs / 1000 / 60 / 60);
			var diffMins = Math.floor(diffMs / 1000 / 60);
			switch (identifier) {
				case "milliseconds":
					return diffMs;
				case "minutes":
					return diffMins;
				case "hours":
					return diffHrs;
				case "days":
					return diffDays;
				default:
					return diffMs;
			}
		}
	},

Any knowledge on how to create this would be greatly appreciated, and thanks again on your previous response! 







 

  1. Only set the "diffHours" once the page is initialized.

Please override the "onEntityInitialized" method and set the "diffHours" there.

2. Go into a while loop checking "while a checkbox is unchecked - run loop" that would set the hours/minutes/seconds remaining every second (or n time to create a "live" feel to the process).

3. Once checkbox is checked, stop entire process and save current time. 

If you need to track when people physically work on an activity then It's a very bad idea. Just let people manage how much time they spend on a task by themselves. 

Anyway, technically it would be more correct to save a current date time value when the checkbox is checked and then save it when the checkbox is unchecked. Then calculate the difference. 

 

Show all comments