Page 1 of 1

oWebView:Eval() in a loop

PostPosted: Thu Feb 16, 2023 2:20 pm
by Natter
Hi,

In the loop, I call oWebView:Eval() and try to process the results in the Result() function. However, it does not work out in any way :(

Code: Select all  Expand view
#include "FiveWin.ch"

function Main()
private oDlg, oWebView

   DEFINE DIALOG oDlg TITLE "This is a dialog" COLOR "N/B" SIZE 400, 200

   @ 3,  8 BUTTON "Test" ACTION Start()

   ACTIVATE DIALOG oDlg ON INIT oDlg:Cargo:={0,0} CENTERED
return nil

function Html()
local cHtml

   TEXT INTO cHtml
      data:text/html,
      <html>
         <head>
            <meta charset="UTF-8" >
         </head>
         <body style="background-color:cyan">
           <button id="bt" style="display:none;">the test is completed</button>
           <script>
              function Test(num) {
                SendToFWH(num) ;
              } ;
              function ViwB() {
                el=document.getElementById("bt").style.display="" ;
              } ;
           </script>
         </body>
      </html>
   ENDTEXT
return cHtml

procedure Start
local st

 if valtype(oWebView)!="O"
   oWebView := TWebView():New()
   oWebView:SetSize(800, 200)
   oWebView:bOnBind = { | cJson, nCalls | Result(cJson) }
   oWebView:Bind( "SendToFWH" )
   oWebView:Navigate( Html() )
 endif

 for st=1 to 5
   oWebView:Eval("Test("+ltrim(str(st))+")" )


? oDlg:Cargo[1], oDlg:Cargo[2]
       oDlg:Cargo[1]:=oDlg:Cargo[2]
 next
? oDlg:Cargo[2]
 oWebView:Eval("ViwB()" )
return

function Result(num)
local dat

  Hb_JsonDecode(num, @dat)
  oDlg:Cargo[2]+=dat[1]

? "C", oDlg:Cargo[2]
return

Re: oWebView:Eval() in a loop

PostPosted: Thu Feb 16, 2023 3:44 pm
by cnavarro
For me run Ok
Image
Please try with my executable
https://bitbucket.org/fivetech/fivewin- ... ebView.exe

Re: oWebView:Eval() in a loop

PostPosted: Thu Feb 16, 2023 4:15 pm
by Natter
Cristobal, thank you for your help, but when trying to download your example, a message appears -
An error when establishing a secure connection
When connected to bbuseruploads.s3.amazonaws.com an error has occurred. The node received a valid certificate, but access was denied.

Re: oWebView:Eval() in a loop

PostPosted: Fri Feb 17, 2023 2:03 am
by cnavarro
Dear Natter
What browser are you using?
It is the download server for Fivetech contributions. No one has had any problems. There are already 13 downloads of that file
If when downloading it warns you that the file may be dangerous, ignore the message and download it.

Re: oWebView:Eval() in a loop

PostPosted: Fri Feb 17, 2023 7:41 am
by Natter
Cristobal, we didn't understand each other. In my example, I tried to show the problem that I had when working with webview.Eval() in a loop. It is as follows -
1. Webview.Eval() executes a certain JS script and returns the result to the FW.
2. Only after receiving this result can I continue executing the loop. It is this check that I do not succeed. :(

Re: oWebView:Eval() in a loop

PostPosted: Fri Feb 17, 2023 8:52 am
by Antonio Linares
Dear Yuri,

Your example works fine here too

Re: oWebView:Eval() in a loop

PostPosted: Fri Feb 17, 2023 12:21 pm
by Natter
In the loop, I call the webview.Eval() method 5 times. Checking the answers looks like this:
1,0 / 2,0 /3,2 / 4,3 / 5,4
Although, as it seems to me, it should be - 1,1 / 2,2 / 3,3 / 4,4 / 5,5

Code: Select all  Expand view
#include "FiveWin.ch"

function Main()
private oDlg, oWebView

   DEFINE DIALOG oDlg TITLE "This is a dialog" COLOR "N/B" SIZE 400, 200

   @ 3,  8 BUTTON "Test" ACTION Start()

   ACTIVATE DIALOG oDlg ON INIT oDlg:Cargo:=0 CENTERED
return nil

local cHtml, flg:=.T.

   TEXT INTO cHtml
      data:text/html,
      <html>
         <head>
            <meta charset="UTF-8" >
         </head>
         <body style="background-color:cyan">
           <button id="bt" style="display:none;">Start process</button>
           <script>
              function Test(num) {
                SendToFWH(num) ;
              } ;
              function Viw(num) {
                el=document.getElementById("bt") ;
                el.innerText="total "+num ;
                el.style.display="" ;
              } ;
           </script>
         </body>
      </html>
   ENDTEXT
return cHtml

procedure Start
local st

 if valtype(oWebView)!="O"
   oWebView := TWebView():New()
   oWebView:SetSize(800, 200)
   oWebView:bOnBind = { | cJson, nCalls | Result(cJson) }
   oWebView:Bind( "SendToFWH" )
   oWebView:Navigate( Html() )
 endif

 for st=1 to 5
   oWebView:Eval("Test("+ltrim(str(st))+")" )
   millisec(1000)

? st, oDlg:Cargo
 next
 oWebView:Eval("Viw("+ltrim(str(oDlg:Cargo))+")" )
return

function Result(num)
local dat

  Hb_JsonDecode(num, @dat)
  oDlg:Cargo:=dat[1]
return