[英]Define global random variable in a JavaScript function
我知道您可能會認為這是一個重復性問題,但不是。 您如何在函數內使用全局變量並保持隨機數? 就像var randomNum = round(random(1, 10))
並說它以2表示,我可以以任何方式保留2嗎?
如果您未在內部函數或javascript文件中使用“使用嚴格”。 因此,您可以定義:
random=round(random(1,10));
它將隨機數作為全局變量。
// randomnum can be accessed here
function yourfunction() {
randomNum = round(random(1, 10);
//can also be accessed here
}
刪除var
auto設置為GLOBAL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.