admin管理员组文章数量:1414614
.js file including an array of Strings which suppose to generate random words when one of the buttons click on the HTML page but for some reason it has a problem of scoping variables (as much as I've understood in other threads) but I don't know how to set it straight.. enter image description here
var interval;
var randomNum;
function newSubject(str) {
clearInterval(interval);
randomNum = Math.floor(Math.random()*(str.length));
interval = setInterval(document.getElementById('generator').innerHTML = str[randomNum],1000);
}
**var adjectives** = [
'מסוגל',
'מקסים',
'הרפתקני',
'חומצי',
'פעיל',
'מפחד',
'מזדקן',
'אגרסיבי',
'נעים',
'ערמומי',
'מדאיג',
'ערני',
'חי',
'מדהים',
'משועשע',
'עתיק',
'מתבייש',
'מושך',
'ממוצע',
'נורא',
];
var food = [
'אוכל',
'קובנה',
'קוסקוס',
'המבורגר',
'לחם',
'גבינה',
'טבעוני',
'צמחוני',
];
var nature = [
'עץ',
'שדה',
'דשא',
'רוח',
'אדמה',
'מים',
'אש',
];
var space = [
'כדור הארץ',
'אטמוספרה',
'ירח',
'כוכבים',
'חללית',
'חלל חיצון',
'חייזר',
'ונוס',
'שמש',
];
var tech = [
'מכונה',
'בינארי',
'אפס',
'אחד',
'מחשב',
'כוח עיבוד',
];
When buttons
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Connection Freestyler</title>
<meta name="description" content="Hebrew Freestyle Generator" />
<meta name="author" content="Connection">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="words.js"></script>
</head>
<body>
<div class="main">
<div class="refresh" onclick="">
Refresh Every 5 Secs
</div>
<div class="generator" id="generator">
Generator "Select Subject"
</div>
<div class="subjects">
**<button class="adjectives" id="adjectives" onclick="newSubject(adjectives)">**
Adjectives
</button>
<button class="food" id="food" onclick="newSubject(food)">
Food
</button>
<button class="nature" id="nature" onclick="newSubject(nature)">
Nature
</button>
<button class="space" id="space" onclick="newSubject(space)">
Space
</button>
<button class="tech" onclick="newSubject(tech)">
Tech
</button>
</div>
</div>
</body>
</html>
.js file including an array of Strings which suppose to generate random words when one of the buttons click on the HTML page but for some reason it has a problem of scoping variables (as much as I've understood in other threads) but I don't know how to set it straight.. enter image description here
var interval;
var randomNum;
function newSubject(str) {
clearInterval(interval);
randomNum = Math.floor(Math.random()*(str.length));
interval = setInterval(document.getElementById('generator').innerHTML = str[randomNum],1000);
}
**var adjectives** = [
'מסוגל',
'מקסים',
'הרפתקני',
'חומצי',
'פעיל',
'מפחד',
'מזדקן',
'אגרסיבי',
'נעים',
'ערמומי',
'מדאיג',
'ערני',
'חי',
'מדהים',
'משועשע',
'עתיק',
'מתבייש',
'מושך',
'ממוצע',
'נורא',
];
var food = [
'אוכל',
'קובנה',
'קוסקוס',
'המבורגר',
'לחם',
'גבינה',
'טבעוני',
'צמחוני',
];
var nature = [
'עץ',
'שדה',
'דשא',
'רוח',
'אדמה',
'מים',
'אש',
];
var space = [
'כדור הארץ',
'אטמוספרה',
'ירח',
'כוכבים',
'חללית',
'חלל חיצון',
'חייזר',
'ונוס',
'שמש',
];
var tech = [
'מכונה',
'בינארי',
'אפס',
'אחד',
'מחשב',
'כוח עיבוד',
];
When buttons
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title>Connection Freestyler</title>
<meta name="description" content="Hebrew Freestyle Generator" />
<meta name="author" content="Connection">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="words.js"></script>
</head>
<body>
<div class="main">
<div class="refresh" onclick="">
Refresh Every 5 Secs
</div>
<div class="generator" id="generator">
Generator "Select Subject"
</div>
<div class="subjects">
**<button class="adjectives" id="adjectives" onclick="newSubject(adjectives)">**
Adjectives
</button>
<button class="food" id="food" onclick="newSubject(food)">
Food
</button>
<button class="nature" id="nature" onclick="newSubject(nature)">
Nature
</button>
<button class="space" id="space" onclick="newSubject(space)">
Space
</button>
<button class="tech" onclick="newSubject(tech)">
Tech
</button>
</div>
</div>
</body>
</html>
I've also tried to put every variable in any scope bination possible but it stick to the same error everytime.
Share Improve this question asked Feb 16, 2020 at 21:06 Dean ShakedDean Shaked 31 gold badge1 silver badge3 bronze badges 2- There's no underscore in your code, so how would we know what the problem is? – Jared Smith Commented Feb 16, 2020 at 21:14
- Ohh my bad , Uncaught ReferenceError: משועשע is not defined at <anonymous>:1:1 There is also a picture link (I'm new to making a post) – Dean Shaked Commented Feb 16, 2020 at 21:21
1 Answer
Reset to default 1setInterval
accept function as parameter. use
interval = setInterval(() => document.getElementById('generator').innerHTML = str[randomNum],1000);
and assuming you mean to make a new random every second, use
document.getElementById('generator').innerHTML = str[randomNum];
interval = setInterval(() => newSubject(str),1000);
snipped:
var interval;
var randomNum;
function newSubject(str) {
clearInterval(interval);
randomNum = Math.floor(Math.random()*(str.length));
document.getElementById('generator').innerHTML = str[randomNum];
interval = setInterval(() => newSubject(str),1000);
}
var adjectives = [
'מסוגל',
'מקסים',
'הרפתקני',
'חומצי',
'פעיל',
'מפחד',
'מזדקן',
'אגרסיבי',
'נעים',
'ערמומי',
'מדאיג',
'ערני',
'חי',
'מדהים',
'משועשע',
'עתיק',
'מתבייש',
'מושך',
'ממוצע',
'נורא',
];
var food = [
'אוכל',
'קובנה',
'קוסקוס',
'המבורגר',
'לחם',
'גבינה',
'טבעוני',
'צמחוני',
];
var nature = [
'עץ',
'שדה',
'דשא',
'רוח',
'אדמה',
'מים',
'אש',
];
var space = [
'כדור הארץ',
'אטמוספרה',
'ירח',
'כוכבים',
'חללית',
'חלל חיצון',
'חייזר',
'ונוס',
'שמש',
];
var tech = [
'מכונה',
'בינארי',
'אפס',
'אחד',
'מחשב',
'כוח עיבוד',
];
<body>
<div class="main">
<div class="refresh" onclick="">
Refresh Every 5 Secs
</div>
<div class="generator" id="generator">
Generator "Select Subject"
</div>
<div class="subjects">
**<button class="adjectives" id="adjectives" onclick="newSubject(adjectives)">**
Adjectives
</button>
<button class="food" id="food" onclick="newSubject(food)">
Food
</button>
<button class="nature" id="nature" onclick="newSubject(nature)">
Nature
</button>
<button class="space" id="space" onclick="newSubject(space)">
Space
</button>
<button class="tech" onclick="newSubject(tech)">
Tech
</button>
</div>
</div>
本文标签: javascriptUncaught ReferenceErroris not defined at ltanonymousgt11 (Not jQuery)Stack Overflow
版权声明:本文标题:javascript - Uncaught ReferenceError: _ is not defined at <anonymous>:1:1 (Not jQuery) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745150791a2644911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论