最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught ReferenceError: _ is not defined at <anonymous>:1:1 (Not jQuery) - Stack Overflow

programmeradmin0浏览0评论

.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
Add a ment  | 

1 Answer 1

Reset to default 1

setInterval 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>

发布评论

评论列表(0)

  1. 暂无评论