JS function returns value but not visible when calling this function from QML
-
wrote on 14 May 2021, 09:24 last edited by
Dear all,
i have this:
main.qml:
import 'JavaScript.js' as JS property int recordsAmount Timer { id: readTimer interval: 5000 running: true repeat: true triggeredOnStart: true onTriggered: { //recordsAmount = JS.httpRequest("offline_count_record.php"); // this doesnt work because it cant get the value, see next line console.log(JS.httpRequest("offline_count_record.php")); // this debugs "undefined" } }
JavaScript.js:
function httpRequest(file) { var url = "http://www.someurl.com/" + file; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState === XMLHttpRequest.DONE && xmlhttp.status === 200) { var arr = JSON.parse(xmlhttp.responseText.toString()); if(arr.result === false) { console.log(arr.error); return false; } else { if(file === "offline_count_record.php") { console.log(arr.count); // this debugs correctly the number, also typeof(arr.count) returns "number", so this is correct return arr.count; } else { return true; } } } } xmlhttp.open("GET", url, false); // false for sync, true for async xmlhttp.send(); }
example of the "arr" returned by xmlhttp is:
{"result":true,"count":158}
so how come the timer tick debugs "undefined" ???
each tick i have in console this:
qml: 158 qml: undefined
where first line (158 value) comes from JS script console.log, and second line ("undefined") comes from Timer tick console.log
-
Dear all,
i have this:
main.qml:
import 'JavaScript.js' as JS property int recordsAmount Timer { id: readTimer interval: 5000 running: true repeat: true triggeredOnStart: true onTriggered: { //recordsAmount = JS.httpRequest("offline_count_record.php"); // this doesnt work because it cant get the value, see next line console.log(JS.httpRequest("offline_count_record.php")); // this debugs "undefined" } }
JavaScript.js:
function httpRequest(file) { var url = "http://www.someurl.com/" + file; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState === XMLHttpRequest.DONE && xmlhttp.status === 200) { var arr = JSON.parse(xmlhttp.responseText.toString()); if(arr.result === false) { console.log(arr.error); return false; } else { if(file === "offline_count_record.php") { console.log(arr.count); // this debugs correctly the number, also typeof(arr.count) returns "number", so this is correct return arr.count; } else { return true; } } } } xmlhttp.open("GET", url, false); // false for sync, true for async xmlhttp.send(); }
example of the "arr" returned by xmlhttp is:
{"result":true,"count":158}
so how come the timer tick debugs "undefined" ???
each tick i have in console this:
qml: 158 qml: undefined
where first line (158 value) comes from JS script console.log, and second line ("undefined") comes from Timer tick console.log
@shokarta I'm not an JS expert but I can't see where function httpRequest(file) returns anything?
-
wrote on 14 May 2021, 09:33 last edited by
@shokarta said in JS function returns value but not visible when calling this function from QML:
return arr.count;
yea just minute ago, i have same thought...
at the moment im invoking JS.httpRequestA(file) from QML file, so it sends the request...
however it does not return value at the same time... therefore the result is undefined :(so i have to find a way to rewrite the JS function to send the request, and do nothing till it returns its valuer to be returned by this JS function back to QML
-
@shokarta said in JS function returns value but not visible when calling this function from QML:
return arr.count;
yea just minute ago, i have same thought...
at the moment im invoking JS.httpRequestA(file) from QML file, so it sends the request...
however it does not return value at the same time... therefore the result is undefined :(so i have to find a way to rewrite the JS function to send the request, and do nothing till it returns its valuer to be returned by this JS function back to QML
wrote on 14 May 2021, 09:46 last edited by@shokarta said in JS function returns value but not visible when calling this function from QML:
so i have to find a way to rewrite the JS function to send the request, and do nothing till it returns its valuer to be returned by this JS function back to QML
I would suggest you to take a look at this example ==> https://gist.github.com/40/3192269
1/4