Problem with exeJavascriptDirect

Workarounds and usability notes.

Problem with exeJavascriptDirect

Postby John Robin Dove » Tue May 12, 2015 8:02 am

Hi Clifton,

I have a problem. I use the current date and time in the name of the folder created on the server to contain the student's work (mp3 and text files). Something like [name of exercise + current date + current time]. The student is not allowed to do an exercise more than once in an hour so when an exercise is opened the folders are checked to make sure there is no folder for this exercise created less than an hour ago. (I know there are other ways to do this but I have chosen to do it this way for compatibility with my previous version of this project.) It works OK but there is one problem. Javascript applies the local time variation when you use the new Date() function or pgDateFormat(). For example here in France this is GMT + 2 hours. That's fine. It's what I want for the folder name. I must then compare the date on the folder with the current date and time and to do this I convert both to milliseconds. Here is the problem: when I use date.parse([date]) the result is not corrected to local time. I thought I had found the solution because there is a Javascript function [date].getTimezoneOffset(). But Powerpac doesn't like it!

This works on an HTML document but not in my Toolbook program:

Code: Select all
function getDate(){
// date1 is the date on the folder
// date2 is the current time
var date1 = "2015-05-12T11:54:00";
var msec1 = Date.parse(date1);
var date2 = new Date();
var localTime = date2.getTimezoneOffset();
var msec2 = Date.parse(date2);
var msec21 = msec2;
var result;
var difference;
// getTimezoneOffset() gives the time difference in minutes
// eg 120
localTime = localTime * 60000
msec2 = msec2 - localTime

if (msec2 < msec1)
{
result = 'folder date > current time';
}
else
{
result = 'current time > folder date';
}

difference = msec2 - msec1;
difference = msToTime(difference);

alert('date and time on folder = ' + date1
+ '\r\n' + 'folder date = ' + msec1 + '\r\n' + 'currentTime = ' + msec2
+ '\r\n' + result + '\r\n' + 'current time without correction: ' + msec21
+ '\r\n' + 'current time corrected: ' + msec2
+ '\r\n' + 'difference = ' + difference);
}

function msToTime(s) {
  var ms = s % 1000;
  s = (s - ms) / 1000;
  var secs = s % 60;
  s = (s - secs) / 60;
  var mins = s % 60;
  var hrs = (s - mins) / 60;
 
  if (hrs < 10) {hrs = "0" + hrs}
  if (mins < 10) {mins = "0" + mins}
  if (secs < 10) {secs = "0" + secs}
  return hrs + ':' + mins + ':' + secs
}


Powepac says: date2.getTimezoneOffset is not a function I've tried changing the variable name but it doesn't make any difference. Could you help please?
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: Problem with exeJavascriptDirect

Postby Clifton » Tue May 12, 2015 8:18 am

The error that pops up will usually tell you what MAY be wrong. At first glance you cannot use a return statement inside a function inside exeJavascriptDirect(). The function does a pretty good job of interpreting statements, but it cannot deal with nesting return statements this way; only ONE return statement can appear in a exeJavascriptDirect() call. Set a variable OUTSIDE the function call to hold the return from function and check its value.

FYI: Also be careful about using hard carriage returns inside code statements as they can be misinterpreted by the function unless they are properly coded.

BTW: Some of what you are trying to do MAY already be provided for in the PowerPac function set.
See ...
  • pgTimer()
  • pgCurrentTime()
  • pgFormatDate()
  • pgDateCalc()
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: Problem with exeJavascriptDirect

Postby John Robin Dove » Tue May 12, 2015 3:11 pm

Hi Clifton,

Thanks for your reply. So basically something like date2.getTimezoneOffset(); cannot be used in exeJavascriptDirect?

I've solved the problem by running the function in an embedded HTML and sending the result back via a user event with the value set appropriately.

I was unable to use any of the Powerpac functions for this but I may not have understood everything. The problem always seemed to be the same: when you ask for a date string using a format like "isoDateTime" the values are corrected to local time but when you use milliseconds you get the time as GMT.

I'm not sure what you mean by "hard carriage returns". The crlfs are replaced by spaces when I paste the code into the Actions Editor. Perhaps crlfs can be a problem if you use "text of field ..."?
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: Problem with exeJavascriptDirect

Postby Clifton » Tue May 12, 2015 3:34 pm

date2.getTimezoneOffset() should work as long as date2 is a valid date object.

Just tried it with exeJavascriptDirect(). Typed the following into a field named "code" and then set exeJavascriptDirect() to run it and the alert showed 300, which is correct for me:

    var x = new Date();
    var z = x.getTimezoneOffset();
    alert( z );

Here is the result:

Image 3.png
getTimezoneOffset() and exeJavascriptDirect()
Image 3.png (18.54 KiB) Viewed 693 times

Worked in both IE and Firefox.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: Problem with exeJavascriptDirect

Postby John Robin Dove » Wed May 13, 2015 4:28 am

Yes, you're right. I've tested it on Chrome too. Unfortunately I have deleted all my previous versions so I'm not sure exactly what I did to trigger the error message date2.getTimezoneOffset is not a function. I haven't been able to reproduce the error. Sorry to have wasted your time.
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron