所以基本上我有一个网页,我已经将JavaFX与之链接,一直运行应用程序一切都很好(可以执行java到javascript,反之亦然),我现在唯一的问题就是当我尝试执行一个具有回调函数的javascript函数时,它返回空值,我试图在网页上放一个按钮并执行相同的功能IT EXECUTED FINE !!我糊涂了!任何人都面临同样的问题,或者只是我? JavaFX Web引擎是否可能不支持回调函数?
所以这是我的网页的javascript代码
function travellingTime(Origin,Destination,callback){
var myVar;
var directionsService = new google.maps.DirectionsService();
var request = {
origin: Origin,
destination: Destination,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
// Display the distance:
//alert(response.routes[0].legs[0].distance.value + " meters");
// Display the duration:
//Time = (response.routes[0].legs[0].duration.value / 60);
myVar = (response.routes[0].legs[0].duration.value / 60);
callback(myVar);
}
});
}
获取“myVar”值所需的javascript回调函数
function getTime(myVar){app.setTime(myVar.toFixed(2));}
请注意“app”设置了一个名为“vehicleRoutingProblem”的类,我正在使用方法setTime()来设置时间
class class vehicleRoutingProblem(忽略糟糕的设计,只是为了练习)public static String time;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
最后我有我的主类,我称之为VehicleRoutingProblem类来打印时间
engine.executeScript("travellingTime('port louis','vacoas',getTime)");
VehicleRoutingProblem vpr = new VehicleRoutingProblem();
System.out.println("Travelling time to portlouis "+vpr.time);
使用executeScript执行此操作给了我
Travelling time to portlouis null
但是,如果页面中嵌入了一个按钮,则会打印出正确的控制台答案
<button onclick="travellingTime('port louis','vacoas',getTime)">TSP</button>