![](/img/trans.png)
[英]How do I put arrays which consist of variables that are attached to URL's into image placeholders? JavaScript
[英]How do I put together string variables in Javascript? It's just saying "Object Undefined"
我已經嘗試了很長時間才能使此代碼正常工作。 當你需要快速時,我正在編寫一個小游戲,所以我做了一個秒表。 但是秒表就是不想工作。 而不是秒表顯示Object Undefined
,我不知道為什么。 這是我正在使用的代碼:
var stopwatchFrame = 0;
var stopwatchSeconds = 0;
var stopwatchSecondsString = "Nothing";
stopwatchFrame+=1;
stopwatchSeconds = floor(stopwatchFrame/updatesPerSecond);
stopwatchSecondsString = toString(stopwatchSeconds);
var = "Total time: " + stopwatchSecondsString + " seconds";
我正在使用一個名為Koda.nu的簡單網站,這是一個供年輕人學習 JS 編程的瑞典網站。 一些功能來自它們的內置源。 我是編程新手,所以這就是原因。
您缺少一個變量名稱,其中您的值為"Total time: " + stopwatchSecondsString + " seconds";
它應該是:
var totalTime = "Total time: " + stopwatchSecondsString + " seconds";
另請閱讀@Jaromanda X 在評論部分中寫的內容。 它應該是這樣的:
stopwatchSeconds = Math.floor
(stopwatchFrame/updatesPerSecond); stopwatchSecondsString = stopwatchSeconds.toString()
;
我們無權訪問您的updatesPerSecond
變量,因此也會引發錯誤。 如果聲明,您的代碼將像這樣工作:
var stopwatchFrame = 0;
var stopwatchSeconds = 0;
var stopwatchSecondsString = "Nothing";
var updatesPerSecond = 0;
stopwatchFrame += 1;
stopwatchSeconds = Math.floor(stopwatchFrame / updatesPerSecond);
stopwatchSecondsString = stopwatchSeconds.toString();
var totalTime = "Total time: " + stopwatchSecondsString + " seconds";
你在最后一行沒有變量名,如果這就是你的全部代碼,那么你不初始化updatesPerSecond,這意味着你沒有像
var updatesPerSecond = somenumberhere
如果你命名你的最后一個變量並初始化 updatesPerSecond 那么你應該沒問題。
但是我對這個網站一無所知,但我懷疑它已經過時了。 這里有一些建議。
您需要告訴 javascript,該樓層是來自 Math 的 function 所以使用Math.floor
,也許它可以像您一樣在這個網站上工作,但請記住,您應該以其他方式使用它。
toString() 不能那樣工作。 同樣,我不知道他們是否使用了一些不同的方法,但是普通的 js toString() 像數字.toString() 一樣工作,你可以將基數作為參數傳遞,這意味着數字表示的基數(2 表示二進制,16 表示十六進制等)但這是可選的,十進制默認為 10。
不要使用 var 作為聲明。 如果變量會更改,請使用 let ,如果不會更改,請使用 const 。 在您的情況下,您應該在任何地方使用 let 。
另一件事是,您可以使用 ++ 運算符將值增加 1,因此不要使用stopwatchFrame+= 1
而是使用stopwatchFrame++
最后,您不應該將默認字符串值初始化為“Nothing”,它應該是“”、空字符串或未定義或 null。
我希望這會有所幫助,祝你有美好的一天!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.