Is it possible to pass a parameter to exeJavascriptDirect?

Workarounds and usability notes.

Is it possible to pass a parameter to exeJavascriptDirect?

Postby John Robin Dove » Sun Jan 18, 2015 10:56 am

Hi Clifton,

I have to display seconds in min:sec format. I can do this using the code below in exeJavacriptDirect but I don't know how to send the seconds instead of using the local variable totalSeconds

var totalSeconds = 64;
totalSeconds %= 3600;
var minutes = Math.floor(totalSeconds / 60);
var seconds = totalSeconds % 60;
if (seconds < 10)
{
seconds = "0" + seconds;
}
return minutes + ":" + seconds;

Any ideas please? I think you may have explained this to me before but I can't remember.
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: Is it possible to pass a parameter to exeJavascriptDirec

Postby Clifton » Sun Jan 18, 2015 8:17 pm

While it is possible to set a variable in exeJavascriptDirect(), what you really need in this scenario is just to use pgDateFormat(). It can take milliseconds as a parameter and convert it for you into hh:mm:ss. Just read the popup help doc and you will see how to do this. If you are trying to convert seconds, then just multiply your seconds value by 1000 to generate a millisecond equivalent for use by the function.

Pretty straightforward really.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: Is it possible to pass a parameter to exeJavascriptDirec

Postby John Robin Dove » Mon Jan 19, 2015 8:38 am

Thanks Clifton,

I missed pgDateFormat. I should have looked for it a bit harder! It does exactly what I need: milliseconds to (hh:)mm:ss I got round the problem by putting a function in an embedded HTML that sets the text of objects in the Toolbook part and calling it via pgExecuteRemote. All unnecessarily complicated! I'll have to simplify it now.

But how do you set a variable in exeJavascriptDirect, please? If it's not too complicated and you have time.
John Robin Dove
 
Posts: 486
Joined: Thu Jan 23, 2014 4:35 am

Re: Is it possible to pass a parameter to exeJavascriptDirec

Postby Clifton » Mon Jan 19, 2015 2:25 pm

Using your example from earlier post ... here is how to set totalSeconds for use with exeJavascriptDirect():
(Put the following string in a field and name the field "jsCode". Then create a global variable in the ToolBook actions system called tmp.)
    var totalSeconds = %1;
    totalSeconds %= 3600;
    var minutes = Math.floor(totalSeconds / 60);
    var seconds = totalSeconds % 60;
    if (seconds < 10) {
    seconds = "0" + seconds;
    }
    return minutes + ":" + seconds;
Notice that I have put in a placeholder string (%1) for the totalSeconds variable. Since exeJavascriptDirect() is merely going to execute the above string as a block of JavaScript, all we need to do is grab the string in the actions editor and then replace our placeholders with the logic that is required at runtime. Essentially the above string becomes a boilerplate from which we make substitutions before calling exeJavascriptDirect().

Notice in the actions below that you must use pgReplace() to make your string substitutions BEFORE exeJavascriptDirect() is run.
    pgReplace( "%1", [seconds to convert], text of field "jsCode", true, true ); store return value in [tmp]
    exeJavascriptDirect( [tmp] ); store return value in [tmp]
Now [tmp] variable equals "[minutes]:[seconds]"

NOTE: exeJavascriptDirect() was not developed to replace creating your own custom functions, but it can be a powerful and dynamic way to execute code on-the-fly in special situations where making a custom js file seems like overkill.
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: Is it possible to pass a parameter to exeJavascriptDirec

Postby John Robin Dove » Mon Jan 19, 2015 3:23 pm

Wonderfully simple! Many thanks!

I've got a couple of other questions on unrelated subjects but they can wait until tomorrow.
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 4 guests

cron