Javascript TypeError:无法调用null的方法“setTextValue”
首先,是的,在我发布这个问题之前,我确实搜索了这个网站,并遵循了一些建议,但仍然得到了相同的结果。我还要告诉你,这段代码很旧,早在2012年就在facebook上运行。我不是开发/编码人员。我确实懂一些php。从我在一些帖子上读到的内容来看,他们通过将js向下移动到接近close标记的位置来实现。这就是我的问题所在。此代码不使用任何头部或身体标记。所以,如果我能解决这个问题,它可能会帮助我解决其他java问题。这是我的代码,它给出了Chrome开发工具中的错误Javascript TypeError:无法调用null的方法“setTextValue”,javascript,php,facebook,Javascript,Php,Facebook,首先,是的,在我发布这个问题之前,我确实搜索了这个网站,并遵循了一些建议,但仍然得到了相同的结果。我还要告诉你,这段代码很旧,早在2012年就在facebook上运行。我不是开发/编码人员。我确实懂一些php。从我在一些帖子上读到的内容来看,他们通过将js向下移动到接近close标记的位置来实现。这就是我的问题所在。此代码不使用任何头部或身体标记。所以,如果我能解决这个问题,它可能会帮助我解决其他java问题。这是我的代码,它给出了Chrome开发工具中的错误 function rech
function recheck(what){
var ajax = new Ajax();
ajax.responseType = Ajax.JSON;
ajax.ondone = function(data) {
if(what=="energy"){
if(data.timeLeft>60){
numEnergySeconds = data.timeLeft
currEnergy = data.value;
did('energy').setTextValue(currEnergy);
if (currEnergy>=maxEnergy){
did('energyTime').setTextValue("");
}
energyCalled=false;//reset this
} else {
setTimeOut(recheck(what),data.timeLeft*1000);
}
} else if(what=="stamina"){
if(data.timeLeft>60){
numStaminaSeconds = data.timeLeft;
currStamina = data.value;
did('stamina').setTextValue(currStamina);
if (currStamina>=maxStamina){
did('staminaTime').setTextValue("");
}
staminaCalled=false;//reset this
} else {
setTimeOut(recheck(what),data.timeLeft*1000);
}
} else if(what=="health"){
if(data.timeLeft>60){
numHealthSeconds = data.timeLeft;
currHealth = data.value;
did('health').setTextValue(currHealth);
if (currHealth>=maxHealth){
did('healthTime').setTextValue("");
}
healthCalled=false;//reset this
} else {
setTimeOut(recheck(what),data.timeLeft*1000);
}
}
}
ajax.onerror = function() {
recheck(what)
}
ajax.post("<?echo $src?>resources/recheck.php", {"what": what});
}
function updateLevels() {
if (currStamina<maxStamina) {
numStaminaSeconds--;
if (numStaminaSeconds<0 && !staminaCalled) {
staminaCalled=true;
did('staminaTime').setTextValue("...")
recheck('stamina');
} else if(!staminaCalled && currStamina<maxStamina){
did('staminaTime').setTextValue(getLevelText(numStaminaSeconds));
}
} else {
did('staminaTime').setTextValue("")
Uncaught TypeError: Cannot call method 'setTextValue' of null
}
if (currEnergy<maxEnergy) {
numEnergySeconds--;
if (numEnergySeconds<0 && !energyCalled) {
energyCalled=true;
did('energyTime').setTextValue("...")
recheck('energy');
} else if (!energyCalled && currEnergy<maxEnergy) {
did('energyTime').setTextValue(getLevelText(numEnergySeconds));
}
} else {
did('energyTime').setTextValue("");
}
if (currHealth<maxHealth) {
numHealthSeconds--;
if (numHealthSeconds<0 && !healthCalled) {
healthCalled=true;
did('healthTime').setTextValue("...")
recheck('health');
} else if (!healthCalled && currHealth<maxHealth) {
did('healthTime').setTextValue(getLevelText(numHealthSeconds));
}
} else {
did('healthTime').setTextValue("");
}
}
function getLevelText( val ) {
var numMinutes = Math.floor(val / 60);
var numSeconds = val % 60;
if (numSeconds<10) numSeconds = "0"+numSeconds;
return ("("+numMinutes+":"+numSeconds+")");
}
我把错误显示在面板上的地方放进去。我知道代码可能不美观,也不是最新的,但我愿意接受关于如何修复此问题的建议,而不必雇佣编码人员重写文件中的js。哪一行产生了错误?你调用setTextValue 16次!发生的情况是您的did“耐力”返回空值。SettXtValue不能对空对象执行任何操作。@Mike我想他引用的是这一行:did'healthTime'。SettXtValue@迈克,我在返回错误的那一行输入了错误。即使它显示了错误,我还是得到了该行的实际值。这就是错误在dev面板中的显示方式。我在它之前和之后都包含代码。}否则的话!staminaCalled&&currstemati还获得一个匿名函数,其代码在这里是这一行,它是同一代码updateLevels的所有部分;如果区间集{setIntervalfunction{UpdateLevel},1000区间集=true;}