Esecuzione funzione Javascript dopo variabili sono impostate
-
20-09-2019 - |
Domanda
Ho le seguenti funzioni:
// NATION
var x, y, z;
function flag(nation,area)
{
x = nation;
this.nation=nation;
var el=document.getElementById("desc");
el.innerHTML='The region you have selected is <b>'+area+'</b>';
document.getElementById("flag").innerHTML='<img src="images/flags/'+nation+'.jpg">';
}
// SERVICE
function output(service)
{
y = service;
this.service=service;
var el=document.getElementById("service-desc");
el.innerHTML='You have selected a <b>'+service+'</b> service.';
document.getElementById("clock").innerHTML='<img src="images/clock-'+service+'.png">';
}
E una volta che l'utente ha impostato le variabili: nazione e servizio è stata impostata, allora vorrei eseguire questa funzione:
function selectmodel()
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Your browser does not support XMLHTTP!");
return;
}
var location=x;
var time=y;
var weight=z;
var url="selectmodel.php";
url=url+'?location='+location+'&time='+time+'&weight='+weight;
xmlhttp.onreadystatechange=stateChanged1;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
window.location.hash="slider";
}
function stateChanged1()
{
if (xmlhttp.readyState==4)
{
document.getElementById("result").innerHTML=xmlhttp.responseText;
}
}
Si prega di qualcuno può essere abbastanza knid per farmi sapere come fare questo?
Grazie, B.
Modifica - dispiace dimenticato di dire che le variabili sono provengono dal di onclick (onClick = "bandiera ( 'scotislands', 'isole scozzesi');")
Soluzione
Credo che si sta cercando - nel vero spirito JavaScript - per un certo tipo di evento che viene attivato, e forse c'è anche un modo, ma in questo caso avevo appena chiamo un assegno da ogni funzione:
var x = null;
var y = null;
var ...
function trySelectModel() {
if (x == null) || (y == null) return;
selectModel(); // Your function
}
function flag(...) {
// Your existing code
trySelectModel();
}
function output(service) {
// Your existing code
trySelectModel();
}
function selectmodel() {
// Your existing code
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow