Android Question Wait for Javascript to complete

tariqyounis

Member
Licensed User
Dear All;
if I run the script I get the last value for para3, is there a way to show each value in turn, like to wait for each script before starting the next one.

Javascript:
    Dim Javascript As String
    Javascript = $"B4A.CallSub('ProcessHTML', true, document.getElementById('para1').value,'para1')"$
    WebViewExtras1.executeJavascript( Javascript)

    Javascript = $"B4A.CallSub('ProcessHTML', true, document.getElementById('para2').value,'para2')"$
    WebViewExtras1.executeJavascript( Javascript)

    Javascript = $"B4A.CallSub('ProcessHTML', true, document.getElementById('para3').value,'para3')"$
    WebViewExtras1.executeJavascript( Javascript)

End Sub

Sub ProcessHTML(Html As String,nm As String)
    ToastMessageShow($"${Html} - ${nm}"$,True)
End Sub
 

drgottjr

Well-Known Member
Licensed User
why don't you simply get all 3 at the same time? you've chosen to limit yourself to 1, but there's no law that says you can't get more. you just need a function that gets all 3 and you return a concatenated string (with a separator) containing the 3 values.
 
Upvote 0

oparra

Well-Known Member
Licensed User
tip see:

B4X:
    Dim Javascript As String
    Javascript = $"document.getElementById('para1').value"$
    Wait For (RunJavaScript(Javascript)) Complete(Html As String)
    Log(Html)


1631647638993.png

It also shows how to get the html string using this useful sub:

B4X:
Private Sub RunJavaScript (js As String) As ResumableSub
    #if B4A
    WebViewExtras1.executeJavascript(WebView1, $"B4A.CallSub('Process_HTML', true, ${js})"$)
    Wait For Process_Html(html As String)
    Return html
    #Else If B4J
    Return WebView1.As(JavaObject).RunMethodJO("getEngine", Null).RunMethod("executeScript", Array(js))
    #Else If B4i
    Dim sf As Object = WebView1.EvaluateJavaScript(js)
    Wait For (sf) WebView1_JSComplete (Success As Boolean, Result As String)
    Return Result
    #end if
End Sub
 
Last edited:
Upvote 1

oparra

Well-Known Member
Licensed User
sample:
B4X:
    Dim Parameters() As String = Array As String("para1", "para2", "para3")
    For Each Parameter As String In Parameters
        Dim Javascript As String = $"document.getElementById('${Parameter}').value"$
        Wait For (RunJavaScript(Javascript)) Complete(Html As String)
        Log($"${Html} - ${Parameter}"$)
    Next
 
Last edited:
Upvote 0

tariqyounis

Member
Licensed User
sample:
B4X:
    Dim Parameters() As String = Array As String("para1", "para2", "para3")
    For Each Parameter As String In Parameters
        Dim Javascript As String = $"document.getElementById('${Parameter}').value"$
        Wait For (RunJavaScript(Javascript)) Complete(Html As String)
        Log($"${Html} - ${Parameter}"$)
    Next
thank you, it is working just fine.
 
Upvote 0
Top