javascript - 页面加载时禁用加载功能

标签 javascript php html function load

我有一些 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/

相关文章:

javascript - 响应字体 : Non-supportive Browsers

php - 关系列在哪里等于?

html - 我可以在单个 css 选择器中覆盖 bootstrap 的默认值吗?

javascript - 数据动画 Bootstrap 轮播效果不起作用

javascript - 页面刷新后输入的值(css)消失了

javascript - 如果 IE11 或 IE10,浏览器会重定向 Vuetify Vue.js 应用程序

javascript - Node.js 中的断开 http.createServer 方法

javascript - 如何从 Mathquill Matheditor 输出为 LaTeX?

php - Codeigniter不显示文件上传错误

php - 函数变成死循环