JavaScript:函数在循环时给出 "is not a function"

标签 javascript function oop loops

当我运行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();

}

控制台中显示以下内容:

Result in console

最佳答案

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

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

关于JavaScript:函数在循环时给出 "is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32082105/

相关文章:

javascript - 需要添加 "child of div"来更改禁用输入

sql - Informix 7.3 中是否有用于测试字母或数字数据的内置函数?

c++ - 类型为 "const char *"的值不能分配给类型为 "char"的实体 C OOP

java - 哪种机械师最合适

javascript - CSS 和 JS 更改按钮悬停时的 div 背景颜色?

javascript - 在一个div中制作超过4个顶点

javascript - 如何使用 Greasemonkey 设计 XML 片段的样式?

c++ - 调用 `strcat` 并能够将较大的字符串存储到较小的字符串中?

c - 函数溢出错误

java - 多线程程序在不同操作系统中的行为