Expand my Community achievements bar.

SOLVED

Custom service reply/Wait or Delay in Javascript

Avatar

Level 2

Hi All,

I have a print button that calls a custom print service via Javascript.  Is it possible to get the code to wait for a reply from the custom service before continuing?  Or, is it possible to put a Wait or Delay in the Javascript?

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Hi Garreth,

The attached demonstrates the JavaScript equivalent of Num2Time.

FormCalc

// form1.page1.formCalc::initialize - (FormCalc, client)

$.rawValue = Num2Time(Time(), "HHMMSS")

JavaScript

// form1.page1.javaScript::initialize - (JavaScript, client)

var d = new Date();

this.rawValue = myScriptObject.pad(d.getHours()) + myScriptObject.pad(d.getMinutes()) + myScriptObject.pad(d.getSeconds());

Since the time getters return a single integer if value < 10, I created a script object to pad the value with a leading 0 (zero),

// form1.page1.#variables[0].myScriptObject - (JavaScript, client)

function pad(n) {

  return (n < 10 ? '0'+ n : n)

}

Steve

View solution in original post

4 Replies

Avatar

Former Community Member

The attached form enables a wait time. It uses 2 hidden fields, one to capture the time the button is clicked and a second to calculate the current time. The script on the button click event is as follows:

// form1.subform1.btn::click - (JavaScript, client)

form1.subform1.timeAtClick.execEvent("calculate");

var endTime = parseInt(form1.subform1.timeAtClick.rawValue) + parseInt(form1.subform1.waitTime.rawValue);

var currentTime = 0;

while (currentTime < endTime) {

     form1.subform1.currentTime.execEvent("calculate");

     currentTime = parseInt(form1.subform1.currentTime.rawValue);

}

xfa.host.messageBox("Done waiting " + form1.subform1.waitTime.rawValue + " seconds.","Timer",3,0);

Steve

Avatar

Level 2

Hi Steve,

Many thanks for your reply.

Since I am using a form guide I cannot use the FormCalc behind timeAtClick and currentTime.  Do you know how to code Num2Time(Time(), "HHMMSS")
in JavaScript?

Garreth

Avatar

Correct answer by
Former Community Member

Hi Garreth,

The attached demonstrates the JavaScript equivalent of Num2Time.

FormCalc

// form1.page1.formCalc::initialize - (FormCalc, client)

$.rawValue = Num2Time(Time(), "HHMMSS")

JavaScript

// form1.page1.javaScript::initialize - (JavaScript, client)

var d = new Date();

this.rawValue = myScriptObject.pad(d.getHours()) + myScriptObject.pad(d.getMinutes()) + myScriptObject.pad(d.getSeconds());

Since the time getters return a single integer if value < 10, I created a script object to pad the value with a leading 0 (zero),

// form1.page1.#variables[0].myScriptObject - (JavaScript, client)

function pad(n) {

  return (n < 10 ? '0'+ n : n)

}

Steve

Avatar

Level 2

Hi Steve,

Thank you for your help.  It work a charm.

Garreth

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----