简体   繁体   English

为什么函数不运行?

[英]Why does function not run?

I have a div which has html code that displays a clock as the user plays the game, as shown below.我有一个 div,它具有 html 代码,可以在用户玩游戏时显示时钟,如下所示。

<td onclick="showClock()" id="clockSection">DAY 1</td>

I wanted to make it such that the time would update (eg from "Day 1" it turns into "Night 1" or "Day 2") would update every time the condition is met.我想让时间更新(例如,从“第 1 天”变成“第 1 天”或“第 2 天”)会在每次满足条件时更新。

I made a function for this, updateClock() which works properly, as expected.我为此创建了一个函数updateClock() ,它按预期正常工作。

 function updateTime() {
  if (minutes >= 60) {
    minutes -= 60
    hours += 1
    updateTime()
  } else if (minutes >= 10) {
    minutesDisplay = minutes
  } else {
    minutesDisplay = "0" + minutes
  }
  if (hours >= 13) {
    hours -= 12
    if (dayHalf === "am") {
      dayHalf = "pm"
    } else {
      dayCount += 1
      dayHalf = "am"
      var clockDisplay = document.getElementById("clockSection")
      clockDisplay.innerHTML = displayTime
    }
  }
  if (hours > 6 && dayHalf === "pm") {
    timeEstimate = "Night"
    displayTime = timeEstimate + " " + dayCount
  } else if (hours < 7 && dayHalf === "am") {
    timeEstimate = "Night"
    displayTime = timeEstimate + " " + dayCount
  } else {
    timeEstimate = "Day"
    displayTime = timeEstimate + " " + dayCount
  }
  if (displayTime !== timeEstimate + " " + dayCount) {
    clock.innerHTML = displayTime
  }
  clock.innerHTML = displayTime
}

Unfortunately, when I wanted to call updateClock() from within my other function beginGame() (which starts the whole game, shown below), the entire beginGame() function did not run.不幸的是,当我想叫updateClock()我的其他功能内beginGame()它开始了整场比赛,如下图所示),整个beginGame()函数没有运行。

function beginGame(){
    updateTime()
...
    //the rest of my code, which when tested without the above updateTime() has NO PROBLEMS.
}

I tried running this and setting breakpoints in both functions, and turns out they both did not even run.我尝试运行它并在两个函数中设置断点,结果它们甚至都没有运行。 However, when I took out the updateTime() from within beginGame() or if I took out the last clock.innerHTML = displayTime it could run properly (unfortunately the result was not what I had wanted).但是,当我从beginGame()取出updateTime()或取出最后一个clock.innerHTML = displayTime它可以正常运行(不幸的是,结果不是我想要的)。 Why might this be so?为什么会这样? I am quite sure I made some syntax error somewhere, but cannot spot it.我很确定我在某处犯了一些语法错误,但无法发现它。 Thanks.谢谢。

Demo of full program is at tdat.byethost10.com.完整程序的演示位于 tdat.byethost10.com。 (The working version was supposed to begin the game upon typing "Begin", like at tdat.mygamesonline.org) Alternatively, here is a WORKING version of what's meant to happen (apologies for the messy HTML) (工作版本应该在输入“开始”后开始游戏,就像在 tdat.mygamesonline.org)或者,这里是一个工作版本的意思(为凌乱的 HTML 道歉)

https://jsfiddle.net/hundotte/vb3je10p/1/ https://jsfiddle.net/hundotte/vb3je10p/1/

Ok.好的。 I have finally solved the problem.我终于解决了这个问题。 It appears to be a slight spelling error, if you look at the JSFiddle you'll spot it.这似乎是一个轻微的拼写错误,如果您查看 JSFiddle,您会发现它。 In the beginGame() function I put the id as "clock" instead of "clockDisplay".beginGame()函数中,我将 id 设为“clock”而不是“clockDisplay”。 Thanks for trying, guys!感谢您的尝试,伙计们! I really appreciate it.对此,我真的非常感激。

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

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