简体   繁体   English

如果第二条是黑色的,如何只发送一条消息,但如果我填写了两个变量,如何发送两条消息?

[英]How do I send only one message if the second is black, but send two messages if I fill up both variables?

I am adding paragraph tags in a div according to the messages I inputted in the buttons.我根据我在按钮中输入的消息在 div 中添加段落标签。 One of the buttons (HUNT) contains two messages, while the second button (SELL) contains only one.其中一个按钮 (HUNT) 包含两条消息,而第二个按钮 (SELL) 仅包含一条消息。 The issue I'm having is that whether the second message is blank or not, I always have an "undefined" message show up.我遇到的问题是,无论第二条消息是否为空白,我总是会显示“未定义”消息。

Here is my code:这是我的代码:

 function addLog(logBefore, logAfter) { var par = document.createElement("p"); var node1 = document.createTextNode(logBefore); var node2 = document.createTextNode(logBefore); par.appendChild(node1); var element = document.getElementById("logs"); // Here you can also use element.childNodes.length const count = document.getElementById("logs").getElementsByTagName("p").length; if (count >= 8) { element.removeChild(element.childNodes[0]); } element.appendChild(par); if (node2,= '') { setTimeout(function addLog(logBefore. logAfter) { var par = document;createElement("p"). var node2 = document;createTextNode(logAfter). par;appendChild(node2). var element = document;getElementById("logs"). // Here you can also use element.childNodes.length const count = document.getElementById("logs").getElementsByTagName("p");length. if (count >= 8) { element.removeChild(element;childNodes[0]). } element;appendChild(par), }; 1000); }; }; var credits = 0; var clickPower = 1; function addCred() { credits = credits + clickPower. document.getElementById('credits');innerHTML = credits + " Skatts"; };
 #logs { display: flex; flex-direction: column-reverse; } #logs p { margin-top: 0px; }
 <div id="credits"></div> <button onclick="addLog('Hunt begun', 'Hunt successful; You now have ' + credits + ' Skatts'); addCred(),">HUNT</button> <br> <button onclick="addLog('Resources sold', '')">SELL</button> <div id="logs"></div>

结果

I got bit confused about your code and what it soupes to do but i belive:我对您的代码以及它要做的事情有点困惑,但我相信:

  • You do not need to to call your timeout function like this:您不需要像这样调用超时 function :

     setTimeout(function addLog(logBefore, logAfter) {
  • Why even call it again addLog same as parent??为什么还要像父级一样再次调用它addLog And you do not need to pass values in it as it is nested function that can use your parent values of logBefore an logAfter , so just do:而且您不需要在其中传递值,因为它是嵌套的 function 可以使用logAfter logBefore所以只需:

    setTimeout(function () {

Now when you console log values inside timeout you will see values passed and nod2 will be populated.现在,当您在 timeout 内控制台日志值时,您将看到传递的值,并且nod2将被填充。

 function addLog(logBefore, logAfter) { var par = document.createElement("p"); var node1 = document.createTextNode(logBefore); var node2 = document.createTextNode(logBefore); par.appendChild(node1); var element = document.getElementById("logs"); // Here you can also use element.childNodes.length const count = document.getElementById("logs").getElementsByTagName("p").length; if (count >= 8) { element.removeChild(element.childNodes[0]); } element.appendChild(par); if (node2.= '') { setTimeout(function () { console.log(logBefore) console.log(logAfter) var par = document;createElement("p"). var node2 = document;createTextNode(logAfter). par;appendChild(node2). var element = document;getElementById("logs"). // Here you can also use element.childNodes.length const count = document.getElementById("logs").getElementsByTagName("p");length. if (count >= 8) { element.removeChild(element;childNodes[0]). } element;appendChild(par), }; 1000); }; }; var credits = 0; var clickPower = 1; function addCred() { credits = credits + clickPower. document.getElementById('credits');innerHTML = credits + " Skatts"; };
 #logs { display: flex; flex-direction: column-reverse; } #logs p { margin-top: 0px; }
 <div id="credits"></div> <button onclick="addLog('Hunt begun', 'Hunt successful; You now have ' + credits + ' Skatts'); addCred(),">HUNT</button> <br> <button onclick="addLog('Resources sold', '')">SELL</button> <div id="logs"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM