我有一些 php、javascript 和 html 代码。我有几行在两个函数中是相同的。当页面加载时,我只想基本上执行其中一个函数,而不是另一个,如何阻止另一个函数执行?下面的例子:
Function 1()
document.getElementById('field1').length=$field1_numrows
Function 2()
document.getElementById('field1').length=$field1_numrows
我不想运行第二个函数,现在第一个函数中出现了一些代码,因此函数 2 实际上覆盖了函数 1 的值。
最佳答案
您所需要的只是某种标志。例如:
var wasExecuted;
function runOnce(){
if( wasExecuted ){
return;
}else{
wasExecuted = true;
document.getElementById('field1').length=$field1_numrows;
}
}
runOnce(); //this will run through the 'else'
runOnce(); //this will exit the runOnce function (as the flag has been set to true)
关于javascript - 页面加载时禁用加载功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20803627/