Javascript 函数被调用太多次

标签 javascript jquery html ecmascript-5

我有一个函数,应该将数字递增 1 在每次单击按钮时。该函数仅在第一次被调用时才能正常工作,然后由于该函数被调用太多次而数量大大增加。 有人可以解释发生了什么,我该如何按照我已经使用的相同模式修复它?

'use strict';
var button = document.getElementsByClassName('button')[0],
    targetElement = document.getElementsByClassName('view-box')[0];

var NumView = function NumView(element, button) {
    this.element = element;
    this.button = button;
};

NumView.prototype.render = function render() {
      this.element.innerHTML = this.num;
      this.button.addEventListener('click', changeValue.bind(this));
      function changeValue() {
      this.num ++;
      this.render();
      }
};

var NumController = function NumController(numView) {
    this.numView = numView;
};

NumController.prototype.initialize = function initialize() {
    this.onLoadShowNum();
};

NumController.prototype.onLoadShowNum = function onLoadShowNum() {
    this.numView.num = 0;
    this.numView.render();
};

(function initialize() {
    var view = new NumView(targetElement, button),
        controller = new NumController(view);
    controller.initialize();
})();
.container {
    width: 100%;
    background: silver;
    text-align: center;
}

.view-box {
    border: 5px solid red;
    padding: 5px;
    background: white;
}

.button {
    line-height: 2.4;
    margin: 25px;
}
<section class="container">
<span class="view-box"></span>
<button class="button">Click!</button>
</section>

最佳答案

每次单击按钮时都会添加一个 eventListener。

就这样吧。

在第一次创建按钮时将按钮与点击事件绑定(bind)。

'use strict';
var button = document.getElementsByClassName('button')[0],
    targetElement = document.getElementsByClassName('view-box')[0];

var NumView = function NumView(element, button) {
    this.element = element;
    this.button = button;
    /* Place it here */
      this.button.addEventListener('click',         changeValue.bind(this));
      function changeValue() {
      this.num ++;
      this.render();
      }
};

NumView.prototype.render = function render() {
      this.element.innerHTML = this.num;
      /*Remove from here*/
      /*
      this.button.addEventListener('click',         changeValue.bind(this));
      function changeValue() {
      this.num ++;
      this.render();
      }
      //This is being called multiple times. 1 + 2 +3 +4+..
      */
};

var NumController = function NumController(numView) {
    this.numView = numView;
};

NumController.prototype.initialize = function initialize() {
    this.onLoadShowNum();
};

NumController.prototype.onLoadShowNum = function onLoadShowNum() {
    this.numView.num = 0;
    this.numView.render();
};

(function initialize() {
    var view = new NumView(targetElement, button),
        controller = new NumController(view);
    controller.initialize();
})();
.container {
    width: 100%;
    background: silver;
    text-align: center;
}

.view-box {
    border: 5px solid red;
    padding: 5px;
    background: white;
}

.button {
    line-height: 2.4;
    margin: 25px;
}
<section class="container">
<span class="view-box"></span>
<button class="button">Click!</button>
</section>

关于Javascript 函数被调用太多次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48598863/

相关文章:

javascript - 为什么服务中的数据没有从指令更新?

javascript - 以编程方式在javascript中创建json数组对象

JavaScript 面向对象 : Object instantiation and adding properties using different syntaxes

javascript - csrf-token POST 405(不允许的方法)Laravel

javascript - 如何按复选框选中属性对表格进行排序

javascript - 使用 css javascript 添加幻灯片

javascript - 为什么我不能 destroy() 这些路标?

jquery - JQuery 中通过名称获取元素的值

jquery - 更改 DIV 内的文本后无法使用 jquery 调整 DIV 大小

jquery - 检测文本输入框值变化