Javascript 错误:未捕获的引用错误:未定义 [函数]
Javascript Error: Uncaught ReferenceError: [function] is not defined
我有两个js文件,都由一个HTML文件调用。
<script type="text/javascript" src="scripts/levelmovement.js"></script>
<script type="text/javascript" src="scripts/generation.js"></script>
水平移动的一部分.js :
function moveLevel(){
if(firstreset == true){
resetTime();
}
var time2 = new Date();
var millis2 = time2.getTime();
var millis3 = millis2 - millis1;
poschange = Math.floor(millis3 / 5);
for(i = 0; i < chunkpos.length; i++){
temppos[i] = chunkpos[i] - poschange;
if(temppos[i] <= -150){
temppos[i] += 1200;
generate(i);
}
pos = temppos[i];
document.getElementById('chunk' + i).setAttribute('style','left: ' + pos + 'px;');
}
}
函数 "moveLevel()" 的调用方式如下:
window.onload = function(){
gameLoop();
}
function gameLoop(){
if(currentscreen == 'playing'){
moveLevel();
}
setTimeout('gameLoop()',1);
}
整整一代.js
:var generatedtop;
var howtogentop = 'gen';
var howtogenbottom = 'gen';
var chunktogenerate = 0;
function topGen(g){
document.getElementById('t' + chunktogenerate).setAttribute('src','images/terrain/t' + g + '.png');
if(g == 'gap0'){
howtogentop = 'gap';
}
else{
howtogentop = 'gen';
}
if(g == 'gap0' || g == 'gap2'){
generatedtop = 'gap';
}
else{
generatedtop = 'default';
}
}
function bottomGen(g){
document.getElementById('b' + chunktogenerate).setAttribute('src','images/terrain/b' + g + '.png');
if(g == 'gap0'){
howtogenbottom = 'gap';
}
else{
howtogenbottom = 'gen';
}
}
function generate(chunknum){
chunktogenerate = chunknum;
var rand1 = Math.floor(Math.random()*100)+1;
var rand2 = Math.floor(Math.random()*100)+1;
if(howtogentop == 'gen'){
if(rand1 <= 25){
topGen('space');
}
if(rand1 <= 50 && rand1 > 25){
topGen('jump');
}
if(rand1 <= 75 && rand1 > 50){
topGen('slide');
}
if(rand1 > 75){
topGen('gap0');
}
}
if(howtogentop == 'gap'){
topGen('gap2');
}
if(howtogenbottom == 'gen'){
if(generatedtop == 'gap'){
if(rand2 <= 33){
bottomGen('space');
}
if(rand2 <= 66 && rand2 > 66){
bottomGen('jump');
}
if(rand2 > 66){
bottomGen('gap0');
}
}
if generatedtop != 'gap'){
if(rand2 <= 25){
bottomGen('space');
}
if(rand2 <= 50 && rand2 > 25){
bottomGen('jump');
}
if(rand2 <= 75 && rand2 > 50){
bottomGen('jump');
}
if(rand2 > 75){
bottomGen('gap0');
}
}
}
if(howtogenbottom == 'gap'){
bottomGen('gap2');
}
}
我已经检查了所有内容,"moveLevel()"只有在我删除这行代码时才有效:
generate(i);
似乎浏览器看不到"生成()"函数,我不知道为什么......
这一行:
if generatedtop != 'gap'){
缺少括号。正确的是:
if(generatedtop != 'gap'){
我来到这个页面寻找我遇到的类似问题的解决方案。虽然这个页面没有直接帮助,但给出了解决问题的方向。
以下是发生这种情况的实际原因:
当你在一个js文件中有一个函数说file1.js,你在另一个js文件中调用说file2.js,即使它们在同一个html页面上被调用,如果整个文件1.js有任何js错误,该函数也不会工作,就像如果它有错误,整个js文件根本不包括在内。
因此,解决方案是清除您在所有包含的js文件中收到的所有js错误。
希望这对某人有所帮助。
这是因为您在定义函数之前调用了函数。更改加载 js 文件的顺序应该可以解决此问题。
<script type="text/javascript" src="scripts/generation.js"></script>
<script type="text/javascript" src="scripts/levelmovement.js"></script>
相关文章:
- webRTC错误未定义配置
- gmaps4rails 2.4.6未捕获引用错误:未定义Gmaps
- 未捕获引用错误:未定义validateform
- 为什么我会出现此错误"未捕获引用错误:未定义标题;
- Sitecore”;未捕获引用错误:未定义MultilistWithSearch;
- Wordpress:未捕获引用错误:未定义速度
- Node.js引用错误:未定义类名
- Replace()产生“;未捕获的类型错误:未定义的不是函数“;当与零一起使用时
- 错误-未定义easeInBounce
- 未捕获的引用错误:未定义<映射键>
- 未捕获的类型错误:未定义不是上的函数.GetElementByID
- Javascript错误:未捕获引用错误:未定义编辑
- javascript onchange取消绑定错误|未定义
- requirejs优化器错误:未定义窗口
- 未捕获引用错误:未定义PrintThisPage
- JSHint 错误:未定义我的应用程序 (W117)
- 当我编写脚本时,它显示引用错误:未定义宽度
- socket.io - 引用错误:未定义 io
- 反应.js:未捕获类型错误:未定义不是一个函数
- 出现“未捕获的类型错误:未定义不是函数”控制台错误