与其复制我正在使用的所有代码,不如举个例子。我有:
<body onload="show()" >
//while show() is a : setInterval (function(){ thing=thing+1;},500);
然后在 HTML 中我有一个按钮:
<button onclick="thingPlus100()">Upgrade</button>
而thingPlus100()
例如:
function thingPlus100() {
thing= thing+100;
}
在循环之外。
这不起作用。那么在这种情况下,您如何制作一个按钮,当按下该按钮时,该按钮的总和为 100,而不会破坏循环?!!!扩展:这是html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index</title>
<link href="gameCSS.css" rel="stylesheet" type="text/css" />
</head>
<script src="PlanetAndStructures.js" type="text/javascript">
</script>
<body onload="show()" >
<p> </p>
<table width="800" border="1">
<tr>
<th scope="col">TGC*</th>
<th scope="col">Radioactives</th>
<th scope="col">Organic</th>
<th scope="col">Metall</th>
<th scope="col">Manpower</th>
<th scope="col">Food</th>
<th scope="col">Militar Power</th>
<th scope="col">Economy</th>
<th scope="col">Lvl</th>
</tr>
<tr>
<td><p id="tottgc">Loading</p></td>
<td><p id="totRadioactives">Loading</P></td>
<td><p id="totOrganic">Loading</p></td>
<td><p id="totMetal">Loading</p></td>
<td><p id="Manpower">Loading</p></td>
<td><p id="Food">Loading</p></td>
<td><p id="MS">Loading</p></td>
<td><p id="ES">Loading</p></td>
<td><p id="Lvl">Loading</p></td>
</tr>
</table>
<p>Planet geology and info:</p>
<table width="800" border="1">
<tr>
<td colspan="7" bgcolor="#00FF99" class="cent">Info</td>
</tr>
<tr>
<td width="128" bgcolor="#00FF99" class="cent">Gravity</td>
<td width="152" bgcolor="#00FF99" class="cent">Natural ecosistem</td>
<td width="142" bgcolor="#00FF99" class="cent">Radioactive</td>
<td width="111" bgcolor="#00FF99" class="cent">Metal</td>
<td width="113" bgcolor="#00FF99" class="cent">Organic</td>
<td width="113" bgcolor="#00FF99" class="cent">Area</td>
<td width="114" bgcolor="#00FF99" class="cent">Solar</td>
</tr>
<tr>
<td bgcolor="#C6FFC6" class="cent"><p id="grav">?</p></td>
<td bgcolor="#C6FFC6" class="cent"><p id="natEc">?</p></td>
<td bgcolor="#C6FFC6" class="cent"><p id="radio">?</p></td>
<td bgcolor="#C6FFC6" class="cent"><p id="metal">?</p></td>
<td bgcolor="#C6FFC6" class="cent"><p id="organ">?</p></td>
<td bgcolor="#C6FFC6" class="cent"><p id="Area">?</p></td>
<td height="23" bgcolor="#C6FFC6" class="cent"><p id="solar">?</p></td>
</tr>
</table>
<p> </p>
<p>Planet indexes </p>
<table width="800" border="1">
<tr>
<td colspan="7" bgcolor="#FFFF99" class="cent">Indexes</td>
<td colspan="6" bgcolor="#FF6600" class="cent">Resource production</td>
</tr>
<tr>
<td bgcolor="#FFFF99" class="cent">Fuel</td>
<td bgcolor="#FFFF99" class="cent">Industry</td>
<td bgcolor="#FFFF99" class="cent">Ecosistem</td>
<td bgcolor="#FFFF99" class="cent">Research</td>
<td bgcolor="#FFFF99" class="cent">Anarchy</td>
<td bgcolor="#FFFF99" class="cent">Production</td>
<td bgcolor="#FFFF99" class="cent">Construction</td>
<td bgcolor="#FF6600" class="cent">Food</td>
<td bgcolor="#FF6600" class="cent">Radioactive</td>
<td bgcolor="#FF6600" class="cent">Organic</td>
<td bgcolor="#FF6600" class="cent">Metals</td>
<td bgcolor="#FF6600" class="cent">Energy </td>
<td bgcolor="#FF6600" class="cent">TGC's</td>
</tr>
<tr>
<td height="23" bgcolor="#FFFFCE" class="cent"><p id="FI">?</p></td>
<td bgcolor="#FFFFCE" class="cent"><p id="II">?</p></td>
<td bgcolor="#FFFFCE" class="cent"><p id="EI">?</p></td>
<td bgcolor="#FFFFCE" class="cent"><p id="RI">?</p></td>
<td bgcolor="#FFFFCE" class="cent"><p id="AI">?</p></td>
<td bgcolor="#FFFFCE" class="cent"><p id="PI">?</p></td>
<td bgcolor="#FFFFCE" class="cent"><p id="CI">?</p></td>
<td bgcolor="#FFC6A4" class="cent"><p id="Fp";</td>
<td bgcolor="#FFC6A4" class="cent"><p id="Rp">?</p></td>
<td bgcolor="#FFC6A4" class="cent"><p id="Op">?</p></td>
<td bgcolor="#FFC6A4" class="cent"><p id="Mp">?</p></td>
<td bgcolor="#FFC6A4" class="cent"><p id="E">?</p></td>
<td bgcolor="#FFC6A4" class="cent"><p id="TGCp">?</p></td>
</tr>
</table>
<p>Planet structures </p><table width="800" border="1">
<tr>
<td width="126" height="42">Solar plants</td>
<td width="423">Encreases planet energy by solar index.<br /><span id="lvlCostSol">
?</span></td>
<td width="151">Energy+</td>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//Im working with this button now---------<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<td width="72"><p id="butSol"><button onclick="SolarLvlPlus()">Upgrade</button></p></td>
</tr>
<tr>
<td>Plutonium plants</td>
<td>Encreases planet energy by fuel index (needs radioactives to run)<br /><span id="lvlCostPlut">?</span></td>
<td><p>Energy++; radioactives-</p>
<p>Ecosystem --</p></td>
<td><p id="butPlut"><button onclick="plutonLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td height="46">Mines</td>
<td>Encreases planet resource mining by industry index.<br /><span id="lvlCostMin">?</span></td>
<td><p>[Metal, Organic, Radioactive ]+;<br />
Ecosystem--</p></td>
<td><p id="butMin"><button onclick="minesLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Urbanitzations</td>
<td>Encreases planet manpower growth by industry index.<br /><span id="lvlCostUrb">?</span></td>
<td>Manpower + ;Ecosystem-</td>
<td><p id="butUrb"><button onclick="UrbanLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Storage Facilities</td>
<td>Encreases planet resource storages by industry index.<br /><span id="lvlCostStor">?</span></td>
<td>Estorage+</td>
<td><p id="butStor"><button onclick="storageLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td height="51">Food productors</td>
<td>Encreases food production by ecosistem Index.<br /> <span id="lvlCostFood">?</span></td>
<td>Food +</td>
<td><p id="butFood"><button onclick="foodProdLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>General Factories</td>
<td>Encreases Industry and construction Index.<br /><span id="lvlCostFac">?</span></td>
<td>Industry+; Construction +, Anarchy +;</td>
<td><p id="butInd"><button onclick="industryLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Markets</td>
<td>Encreases TGC production.<br /><span id="lvlCostMar">?</span></td>
<td>TGC+</td>
<td><p id="butMark"><button onclick="marketLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Space industries</td>
<td>Encreases production Index.<br /><span id="lvlCostSpc">?</span></td>
<td>Production +</td>
<td><p id="butSpaInd"><button onclick="spaceIndLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Ecosistem modulators</td>
<td>encreases Ecosistem Index.<br /><span id="lvlCostEco">?</span></td>
<td>Ecosystem ++</td>
<td><p id="butEco"><button onclick="ecoModLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td height="23">Artificial nutrishment</td>
<td>encreases food production by research and industrial Index.<br /><span id="lvlCostArtF">?</span></td>
<td>Food ++; Anarchy +</td>
<td><p id="butArtNut"><button onclick="artNutLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Order centers
</td>
<td>Decreases Anarchy Index.<br /><span id="lvlCostOrd">?</span></td>
<td>Anarchy --</td>
<td><p id="butOrd"><button onclick="OrderLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Particle accelerators</td>
<td>produces radioactive materials.<br /> <span id="lvlCostPart">?</span></td>
<td>Fuel ++, Ecosystem-</td>
<td><p id="butPart"><button onclick="particleLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Geomorph</td>
<td>increases usable area.<br /><span id="lvlCostGeoM">?</span></td>
<td>Area+, Ecosystem -</td>
<td><p id="butGeo"><button onclick="geomorphLvl+ ()">Upgrade</button></p></td>
</tr>
<tr>
<td>Science Investments</td>
<td>Increase research index.<br /><span id="lvlCostRes">?</span></td>
<td>Research+</td>
<td><p id="butRes"><button onclick="researchLvl+ ()">Upgrade</button></p></td>
</tr>
</table>
<p id="prodPH">?</p>
<p>*:Trans-Galactic Crown (money).</p>
<p> </p>
</html>
</body>
这是.js:
//PLANET UNMOBILE STATISTICS:
var plaMetal=Math.floor((Math.random()*10)+5);
var plaArea =Math.floor((Math.random()*500)+50);
var plaGrav =Math.floor((Math.random()*8)+3);
var plaEco =Math.floor((Math.random()*71)+30);
var plaRad =Math.floor((Math.random()*10)+3);
var plaOrg =Math.floor((Math.random()*10)+4);
var plaSol =Math.floor((Math.random()*250)+50);
//STRUCTURE LEVELS;;
var artifNoutrishmentLvl=0;
var ecoModulatorsLvl=0;
var foodProductorsLvl=1;
var genFactoiesLvl=1;
var geomorphLvl=0;
var marketLvl=1;
var minesLvl=1;
var orderCenterLvl=1;
var particlesLvl=0;
var plutLvl=0;
var scienceInvestLvl=1;
var solarPlantsLvl=1;
var spcIndustriesLvl=1;
var storageLvl=1;
var urbanitzationLvl=1;
//OTHERS
var buildings=0;
var shipProd=0;
var game=1;
var solarConstr=0;<<<<<<<<<<<<<<<<<<<<<<<<<<this is the variable!!!
//INDEXES:
var industryI=(((genFactoiesLvl*3)+(particlesLvl+urbanitzationLvl/2))/4)+1;
var fuelI= ((industryI + urbanitzationLvl + particlesLvl)/4)+1;
var ecosistemI= plaEco-(plutLvl*3)-(minesLvl*2)- urbanitzationLvl-particlesLvl-geomorphLvl+(ecoModulatorsLvl*4);
var researchI=scienceInvestLvl*5;
var anarchyI=(genFactoiesLvl*2)+ (artifNoutrishmentLvl*3) +urbanitzationLvl-(orderCenterLvl*4);
var Production=(genFactoiesLvl*2)+(spcIndustriesLvl*4);
var constructionI=(genFactoiesLvl*5);
//RESOURCE PRODUCTION PER HOUR
var foodPH=((foodProductorsLvl*plaEco)+ artifNoutrishmentLvl*10)*15/2 ;
var radPH=((minesLvl*plaRad*industryI*2)+(particlesLvl*industryI*5)*20)/2;
var metPH=((minesLvl*plaMetal*industryI*1.5)*19)/2;
var orgPH=((minesLvl*(plaOrg/5)*industryI*3)*20)/2;
var tgcPH=(((marketLvl*8)+industryI+urbanitzationLvl)*10)/2;
//RESOURCES
var energy= Math.round(solarE + plutoniumE - buildings - shipProd);
var solarE =Math.round((plaSol*solarPlantsLvl)/10)+5;
var plutoniumE=Math.round((plutLvl*fuelI)/3)+10;
var radioactive =100;
var organics=400;
var metals=600;
var tgcs =200;
var food=500;
var manpower =Math.round((urbanitzationLvl*industryI)/10);
var energy= Math.round(solarE + plutoniumE - buildings - shipProd);
//COSTS:
//Metal
var artifNoutMCost= Math.round(((artifNoutrishmentLvl^2)/4)*200)+ 500;
var ecoModulatorsMCost= Math.round(((ecoModulatorsLvl^2)/2)*350)+80;
var foodProductorsMCost= Math.round(((foodProductorsLvl^2)/20)*15)+25;
var genFactoiesMCost= Math.round(((genFactoiesLvl^3)/13)*50)+50;
var geomorphMCost= Math.round(((geomorphLvl^2)/5)*250)+500;
var marketMCost= Math.round(((marketLvl^2)/10)*45)+54;
var minesMCost= Math.round(((minesLvl^3)/20)*60)+45;
var orderCenterMCost= Math.round(((orderCenterLvl^2)/10)*25)+100;
var particlesMCost= Math.round((particlesLvl^4)*50)+550;
var plutMCost= Math.round(((plutLvl^3)/25)*12)+100;
var scienceInvestMCost= Math.round(((scienceInvestLvl^2)/10)*25)+55;
var solarPlantsMCost= Math.round(((solarPlantsLvl^2)/10)*20)+40;
var spcIndustriesMCost= Math.round(((spcIndustriesLvl^3)/3)*55)+250;
var storageMCost= Math.round(((storageLvl^2)/10)*12)+ 160;
var urbanitzationMCost= Math.round(((urbanitzationLvl^2)/12)*25)+45;
//ORGANIC
var artifNoutOCost= Math.round(((artifNoutrishmentLvl^3)/3)*250)+500;
var ecoModulatorsOCost= Math.round(((ecoModulatorsLvl^2)/2)*350)+280;
var foodProductorsOCost= Math.round(((foodProductorsLvl^2)/10)*25)+35;
var genFactoiesOCost= Math.round(((genFactoiesLvl^3)/15)*30)+30;
var geomorphOCost= Math.round(((geomorphLvl^2)/2)*400)+600;
var marketOCost= Math.round(((marketLvl^2)/10)*45)+54;
var minesOCost= Math.round(((minesLvl^2)/20)*30)+50;
var orderCenterOCost= Math.round(((orderCenterLvl^2)/20)*25)+50;
var particlesOCost= Math.round((particlesLvl^2)*5)+110;
var plutOCost= Math.round(((plutLvl^2)/50)*6);
var scienceInvestOCost= Math.round(((scienceInvestLvl^3)/10)*25)+80;
var solarPlantsOCost= Math.round(((solarPlantsLvl^2)/19)*15)+20;
var spcIndustriesOCost= Math.round(((spcIndustriesLvl^3)/5)*40)+300;
var storageOCost= Math.round(((storageLvl^2)/10)*12)+ 160;
var urbanitzationOCost= Math.round(((urbanitzationLvl^2)/6)*40)+25;
//RADIOACTIVES
var artifNoutRCost= 0;
var ecoModulatorsRCost= Math.round(((ecoModulatorsLvl^2)/4)*450)+40;
var foodProductorsRCost=0;
var genFactoiesRCost= Math.round(((genFactoiesLvl^2)/33)*40)+30;
var geomorphRCost= Math.round(((geomorphLvl^2)/5)*150)+300;
var marketRCost= Math.round(((marketLvl^2)/10)*45)+54;
var minesRCost= Math.round(((minesLvl^2)/30)*50)+35;
var orderCenterRCost=0;
var particlesRCost= Math.round((particlesLvl^4)*110)+750;
var plutRCost= Math.round(((plutLvl^3)/15)*42)+240;
var scienceInvestRCost= Math.round(((scienceInvestLvl^2)/15)*30)+25;
var solarPlantsRCost= Math.round(((solarPlantsLvl^2)/40)*10)+10;
var spcIndustriesRCost= Math.round(((spcIndustriesLvl^3)/3)*55)+250;
var storageRCost= Math.round(((storageLvl^2)/10)*12)+ 160;
var urbanitzationRCost= 0;
//TIMES:
var solarTime;
function show(){
setInterval (function(){
//Planet geology and info table:
document.getElementById("metal").innerHTML= plaMetal;
document.getElementById("Area").innerHTML= plaArea;
document.getElementById("grav").innerHTML= plaGrav;
document.getElementById("natEc").innerHTML= plaEco + "%";
document.getElementById("radio").innerHTML= plaRad;
document.getElementById("organ").innerHTML= plaOrg;
document.getElementById("solar").innerHTML= plaSol;
document.getElementById("Area").innerHTML= plaArea;
//Planet indexes table
document.getElementById("E").innerHTML= Math.round(energy);
document.getElementById("Fp").innerHTML= Math.round(food);
document.getElementById("Rp").innerHTML= Math.round(radioactive);
document.getElementById("Op").innerHTML= Math.round(organics);
document.getElementById("Mp").innerHTML= Math.round(metals);
document.getElementById("TGCp").innerHTML= Math.round(tgcs);
document.getElementById("Manpower").innerHTML= manpower;
document.getElementById("FI").innerHTML=Math.round( fuelI);
document.getElementById("II").innerHTML=Math.round( industryI);
document.getElementById("EI").innerHTML= ecosistemI;
document.getElementById("RI").innerHTML= researchI;
document.getElementById("AI").innerHTML= anarchyI;
document.getElementById("PI").innerHTML= Production;
document.getElementById("CI").innerHTML=constructionI;
//COSTS AND LVLS
document.getElementById("lvlCostArtF").innerHTML="Current Lvl: " + artifNoutrishmentLvl + "Cost: " + artifNoutMCost + "Met; " + artifNoutOCost + "Org; " + artifNoutRCost + "Rad." ;
document.getElementById("lvlCostEco").innerHTML="Current Lvl: "+ecoModulatorsLvl+"Cost: "+ecoModulatorsMCost+"Met; "+ecoModulatorsOCost+"Org; "+ecoModulatorsRCost+"Rad.";
document.getElementById("lvlCostFood").innerHTML="Current Lvl: "+foodProductorsLvl+"Cost: "+foodProductorsMCost+"Met; "+foodProductorsOCost+"Org; "+foodProductorsRCost+"Rad.";
document.getElementById("lvlCostFac").innerHTML="Current Lvl: "+genFactoiesLvl+"Cost: "+genFactoiesMCost+"Met; "+genFactoiesOCost+"Org; "+genFactoiesRCost+"Rad.";
document.getElementById("lvlCostGeoM").innerHTML="Current Lvl: "+geomorphLvl+"Cost: "+geomorphMCost+"Met; "+geomorphOCost+"Org; "+geomorphRCost+"Rad.";
document.getElementById("lvlCostMar").innerHTML="Current Lvl: "+ marketLvl+"Cost: "+marketMCost+"Met; "+marketOCost+"Org; "+marketRCost+"Rad.";
document.getElementById("lvlCostMin").innerHTML="Current Lvl: "+minesLvl+"Cost: "+minesMCost+"Met; "+minesOCost+"Org; "+minesRCost+"Rad.";
document.getElementById("lvlCostOrd").innerHTML="Current Lvl: "+orderCenterLvl+"Cost: "+orderCenterMCost+"Met; "+orderCenterOCost+"Org; "+orderCenterRCost+"Rad.";
document.getElementById("lvlCostPart").innerHTML="Current Lvl: "+particlesLvl+"Cost: "+particlesMCost+"Met; "+particlesOCost+"Org; "+particlesRCost+"Rad.";
document.getElementById("lvlCostPlut").innerHTML="Current Lvl: "+plutLvl+"Cost: "+plutMCost+"Met; "+plutOCost+"Org; "+plutRCost+"Rad.";
document.getElementById("lvlCostRes").innerHTML="Current Lvl: "+scienceInvestLvl+"Cost: "+scienceInvestMCost+"Met; "+scienceInvestOCost+"Org; "+scienceInvestRCost+"Rad.";
document.getElementById("lvlCostSol").innerHTML="Current Lvl: "+solarPlantsLvl+"Cost: "+solarPlantsMCost+"Met; "+solarPlantsOCost+"Org; "+solarPlantsOCost+"Rad.";
document.getElementById("lvlCostSpc").innerHTML="Current Lvl: "+spcIndustriesLvl+"Cost: "+spcIndustriesMCost+"Met; "+spcIndustriesOCost+"Org; "+spcIndustriesRCost+"Rad.";
document.getElementById("lvlCostStor").innerHTML="Current Lvl: "+storageLvl+"Cost: "+storageMCost+"Met; "+storageOCost+"Org; "+storageRCost+"Rad.";
document.getElementById("lvlCostUrb").innerHTML="Current Lvl: "+urbanitzationLvl+"Cost: "+urbanitzationMCost+"Met; "+urbanitzationOCost+"Org; "+urbanitzationRCost+"Rad.";
document.getElementById("prodPH").innerHTML="metal: "+metPH+" organics: "+orgPH+" radioactives: "+radPH+" tgc's: "+tgcPH+" food:"+foodPH;
//PRODUCTION ENGINE
radioactive=radioactive+(radPH/7200);
organics=organics+(orgPH/7200);
metals=metals+(metPH/7200);
tgcs=tgcs+(tgcPH/7200);
food=food+(foodPH/7200);
//this is what it should do when the button is pressed
if(solarConstr=1){
}else if(metals>=solarPlantsMCost){
}else if(radioactive>=solarPlantsRCost){
}else if (organics>=solarPlantsOCost){
metals=metals-solarPlantsMCost;
radioactives=radioactives-solarPlantsRCost;
organics=organics-solarPlantsOCost;
solarTime=((solarPlantsOCost+solarPlantsRCost+solarPlantsMCost)*500)/constructionI;
setTimeout(solarTime);
}else if(solarTime>0){
solarTime=-1;
}else if(solarTime==0){
solarPlantsLvl=+1;
solarConstr=-1;
}
},500);}
//here is the extern function
function SolarLvlPlus()
{
solarConstr=solarConstr+1;
}