Skip to content

Instantly share code, notes, and snippets.

@fsans
Last active July 8, 2022 11:30
Show Gist options
  • Save fsans/8050e83d73f89e4854f68e4b498fca01 to your computer and use it in GitHub Desktop.
Save fsans/8050e83d73f89e4854f68e4b498fca01 to your computer and use it in GitHub Desktop.
Callback after javascript module full loaded, using a timed interval

onLoad Callback FileMaker WebViewer

Callback after javascript module full loaded, using a timed interval

$(function () {
  /* send a delayed onLoadEvent to request data preloading */
  const maxWaitingTime = 2000;
  const waitingTime = 200;
  var ctime = 0;
  const waitTimer = () => {
    console.log("wait " + waitingTime + "ms...");
    setTimeout(() => {
      if(typeof FileMaker != 'object'){ 
        ctime += waitingTime;
        ctime>maxWaitingTime ? console.log("can't load FileMaker object") : waitTimer();
      }else{
        console.log("FileMaker lib loaded");
        fmCallback({ error: null, type: "onBrowserReady", data: null, token: null });
      }
    }, waitingTime);
  }
  console.log("Loaded module, waiting data...");
  waitTimer();
});

fmCallback is a FileMaker.PerformScriptWithOption wrapper, like this...

// safe FM callback
const fmHandlerScript = "your-FilemakerScript-callback-handler";
const fmExecuteMode = 0;
// see execution mode option here https://help.claris.com/en/pro-help/content/scripting-javascript-in-web-viewers.html

function fmCallback(param) {
    if(typeof FileMaker != 'object'){
        console.warn("Not in FileMaker Context", param);
    } else {
        try{
            FileMaker.PerformScriptWithOption( fmHandlerScript, JSON.stringify(param), fmExecuteMode );
            // old style callback
            //FileMaker.PerformScript( fmHandlerScript, JSON.stringify(param) );
        }catch(error){
            console.error("Error calling FM:", error, param);
        } 
    } 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment