8 Replies Latest reply on May 7, 2015 6:40 PM by anchorbuoy

    Using javascript setTimeout to send text to FileMaker?

    anchorbuoy

      I’m trying to use setTimeout to facilitate communication between a web viewer and FileMaker, but having some issues sending a parameter to setTimeout in a loop.

       

      This is the loop function, but every time it runs it just repeats the highest number ("10") 10 times. I tried adopting the "bind" method from http://stackoverflow.com/questions/1190642/how-can-i-pass-a-parameter-to-a-settimeout-callback but either I'm implementing it incorrectly or it's not compatible inside a webviewer (on Mac, at least).

       

      function writeCount(){

        for ( i=0; i < 10 ; i++) {

        setTimeout(

        function(){

        sendText(i);

        document.getElementById('countBox').innerHTML=document.getElementById('countBox').innerHTML + i + '<br/>';

        } ,

        i * 150

        );

        }

      }

       

      For reference (although it's not important to the core problem), this is the javascript function that sends information back into FileMaker via the FMP protocol. This is fired from within a webviewer, and I've confirmed it works in both Windows and Mac.

       

      function sendText(text) { window.location="fmp://$/jsCallback?script=writeme(text)&param="+text; }

       

      The sample file is attached. Any suggestions?

       

      Thanks!

      ~ Cristos

        • 1. Re: Using javascript setTimeout to send text to FileMaker?
          steve_ssh

          Hello Cristos,

           

          From the looks of it, this is just a matter of needing to add a closure to your Javascript code to make it work.  I do not believe that the issue is related to FMP or a webviewer.

           

          Here is some sample code, based on your post, which I hope will illustrate this if you try it out:

           

           

           

          // Function similar to your original function

          function writeCount(){

              for ( i=0; i < 10 ; i++) {

                  setTimeout( function(){ console.log(i); }, i * 150 );

              }

          }


           

          // Function re-written with an added closure

          function writeCount02(){

              for ( i=0; i < 10 ; i++) {    

                  (function() {

                      var index = i;

                      setTimeout( function(){ console.log(index); }, index * 150 );

                   })();

              }

          }

           

           

          As you have discovered:


          Without the added closure, each timeout call will be invoked using the last value that variable i was set to in the loop where you set up the timeouts.


          With the added closure, roughly speaking, each iteration of the loop gets its own scoped value for the variable index, and thus, each timeout invocation gets its own scoped value to use.  Without this sort of mechanism in place, e.g. in the first function definition above, all of the timeouts are based on a single variable within the same scope, and thus they all share the same value (which happens to be the last value that was set).


          John Resig has a nice explanation about this type of scenario in one of his older books (I think it's called something like Pro Javascript Techniques).  I'm sure there are a number of other good explanations out there if you search for posts which explain Javascript closures and variable scope.



          If you re-write your function based on the second example above:


          I believe that you should get the result you are looking for.  If it's your first time using an anonymous function in this manner, just pay close attention to the exact placement of all of the parenthesis and braces, and you should be fine.


          HTH & kind regards,


          -steve

          • 2. Re: Using javascript setTimeout to send text to FileMaker?
            anchorbuoy

            Thanks for the suggestion, I'll give it a shot and post here with results!

            • 3. Re: Using javascript setTimeout to send text to FileMaker?
              sam_oda

              Another way. Try this!

              function writeCount(){

                 var i=0;

                 var timer=setInterval(function(){

                countbox.innerHTML+=(i+++"<br>");

                 if(i>9){

                 clearInterval(timer);

                }

                },150);

              }

              writeCount();

               

              /************************************/

              setTimeout / setInterval function is asychronous. So varible i increment occurs first, then setTimeout-functions follows.

              Try the code below.

              function writeCount(){

                 for ( i=0; i < 10 ; i++) {alert(i)

                 setTimeout(function(){alert("timeout"+i);

               

                 document.getElementById('countBox').innerHTML=document.getElementById('countBox').innerHTML + i + '<br/>';

                } ,

                 i * 150
                 );

                }

              }

              writeCount()

              • 4. Re: Using javascript setTimeout to send text to FileMaker?
                Devon Braun

                Parameters can be passed after the timeout value.

                 

                setTimeout([Function],milliseconds,parameter1,parameter2,etc);

                 

                e.g.

                 

                setTimeout(function(){

                     Param1=arguments[0];

                     Param2=arguments[1];

                     console.log(Param1+Param2);

                     //console: "cristoslc"

                },1000,"cristos","lc");

                • 5. Re: Using javascript setTimeout to send text to FileMaker?
                  user19752

                  This run on IE10 but not IE9 (tested on IE11 debugger), so many of Webviewer on windows can't run. Mac may be ok.

                   

                  function writeCount(){

                      for ( i=0; i < 10 ; i++) {

                          setTimeout( function(i){ console.log(i); }, i * 150, i );

                      }

                  }

                  • 7. Re: Using javascript setTimeout to send text to FileMaker?
                    user19752

                    Thanks, it is also written in link in 1st post.

                    Definite answer is in link to MSDN in your link,

                    setTimeout method (Windows)

                    the 3rd parameter is used different in IE, specifying script language...

                    I wonder seeing this, IE10 mode use it as parameter. The page don't show changes in IE version.

                     

                    Editing registry for FM13, my code passed test on windows, but need IE11 mode.

                    HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\FileMaker Pro.exe=2af8(11000)

                    • 8. Re: Using javascript setTimeout to send text to FileMaker?
                      anchorbuoy

                      Sorry for the long silence, my day job's been keeping me too busy to sit down and properly test this. I believe I did have some initial success adapting the method from steve_ssh in the post above, but ran into an unexpected issue in the Windows environment.

                       

                      If anyone wants to take my code and build forward from there, it's on Github at cristoslc/fmp_friendlyHtmlEditor · GitHub

                       

                      When I have some spare cycles, I do hope to resume working on this problem (unless someone else beats me to it!) :-)