【问题标题】:JavaScript: Functions give "is not a function" when looped [duplicate]JavaScript:函数在循环时给出“不是函数”[重复]
【发布时间】:2015-11-11 23:05:43
【问题描述】:

当我运行 tick() 函数时,它可以完美运行,但是当我使用 setInterval(this.tick,this.interval) 循环它时,tick() 中的所有函数都会出现以下错误:Uncaught TypeError: this.{a function} is not a function

这是完整的代码:

function Particles(settings){
this.canvas = document.getElementById('particle-js');
this.context = this.canvas.getContext('2d');
this.fps = 60;
this.interval = 1000/this.fps;
this.TWO_PI = Math.PI*2;
this.mousePosition;


this.tick = function(){
    console.log("tick");


    this.drawNodes();
    this.clear();

}

setInterval(this.tick,this.interval);

this.generateNodes = function generateNodes(){
    var nodes = [];


    for(var i = 0; i < 1; i++){
        var node = {
            origin: {
                x: this.random(0, this.canvas.width),
                y: this.random(0, this.canvas.height)
            },
            direction: this.random(0,359),
            subNodes: []
        }
        for(var j = 0; j < this.random(1,8); j++){
            var subNode = {
                xOffset: this.random(-50,50),
                yOffset: this.random(-50,50),
                distance: function(){
                    var x = Math.pow(this.xOffset,2);
                    var y = Math.pow(this.yOffset,2);
                    return Math.sqrt(x + y);
                }
            }
            while(subNode.distance() < 20){
                subNode = {
                    xOffset: this.random(-50,50),
                    yOffset: this.random(-50,50),
                    distance: function(){
                        var x = Math.pow(this.xOffset,2);
                        var y = Math.pow(this.yOffset,2);
                        return Math.sqrt(x + y);
                    }
                }
            }
            node.subNodes.push(subNode);
        }
        nodes.push(node);
    }

    console.log("generated: " + nodes.length);

    return nodes;
}

this.canvas.addEventListener('mousemove', function(evt) {
    if(evt.offsetX) {
        mouseX = evt.offsetX;
        mouseY = evt.offsetY;
    }
    else if(e.layerX) {
        mouseX = evt.layerX;
        mouseY = evt.layerY;
    }
    this.mousePos = {
        x: mouseX,
        y: mouseY
    }
}, false);

this.drawNodes = function drawNodes(){
    for(var k = 0; k < this.nodes.length; k++){
        var current_node = this.nodes[k];
        this.fillCircle(current_node.origin.x,current_node.origin.y, 3);

        for(var l = 0; l < current_node.subNodes.length; l++){
            var current_subNode = current_node.subNodes[l];

            this.fillLine(current_node.origin.x,current_node.origin.y,current_node.origin.x + current_subNode.xOffset, current_node.origin.y + current_subNode.yOffset)
            this.fillCircle(current_node.origin.x + current_subNode.xOffset, current_node.origin.y + current_subNode.yOffset, 2);
        }
    }
}

this.fillCircle = function fillCircle(x, y, radius, color){
    if(color == undefined || color == false){
        color = "black";
    }
    this.context.fillStyle = color;
    this.context.beginPath();
    this.context.arc(x,y,radius,0,this.TWO_PI,false);
    this.context.fill();
}
this.fillLine = function fillLine(xFrom, yFrom, xTo, yTo, color){
    if(color == undefined || color == false){
        color = "black";
    }
    this.context.beginPath();
    this.context.moveTo(xFrom,yFrom);
    this.context.lineTo(xTo, yTo);
    this.context.lineWidth = 1;
    this.context.stroke();
}
this.clear = function clear(color){
    if(color == undefined || color == false){
        color = "white";
    }
    this.context.fillStyle = color;
    this.context.fillRect(0,0,this.canvas.width,this.canvas.height);
}

this.random = function random(min, max){
    return min+Math.round((max-min)*Math.random());
}

this.nodes = this.generateNodes();

}

控制台显示如下:

【问题讨论】:

  • 因为调用tick()setInterval(this.tick) 不是一回事。上下文不同
  • 谢谢,一切正常

标签: javascript function oop loops


【解决方案1】:

这是因为 setTimeout 回调函数的上下文未绑定到您的对象,请尝试以下操作:

setInterval(this.tick.bind(this), this.interval);

【讨论】:

  • 谢谢!!!它运行良好
  • 没问题,很高兴为您提供帮助!
猜你喜欢
  • 2019-08-18
  • 2015-08-14
  • 2017-07-31
  • 2021-07-15
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-01-26
相关资源
最近更新 更多