简体   繁体   English

我有一个JavaScript范围错误,我不知道如何解决此问题

[英]I have a javascript scope error, I don't know how to fix this

the browser is telling me in 'Task.prototype.start' 'this.clear' is undefined... Here is the class (function) and it's prototypes: 浏览器在'Task.prototype.start'中告诉我'this.clear'是未定义的...这是类(函数)及其原型:

function Task(_func, _ms, _repeats, _args)
{
    if (typeof _func != 'function') throw '_func is not `function`';
    if (typeof _ms != 'number') throw '_ms is not `number`';
    if (typeof _repeats != 'number') _repeats = 0; // default: no repeats, run once. optional
    // _args is optional
    this.func = _func;
    this.ms = _ms;
    this.repeats = _repeats;
    this.args = _args;
    this.tID = 0;
    this.runCounter = 0;
}

Task.prototype.isRunning = function()
{
    return (this.tID != 0);
};
Task.prototype.runsOnce = function(){
    return (this.repeats == 0);
};
Task.prototype.clear = function()
{
    if (this.isRunning())
    {
        if (this.runsOnce())
        {
            clearTimeout(this.tID);
        }
        else
        {
            clearInterval(this.tID);
        }
    }
    this.tID = 0;
    this.runCounter = 0;
};
Task.prototype.start = function()
{
    this.clear();
    var _this = this;
    var _exec = function()
    {
        if (_this.runsOnce())
        {
            _this.func(_this.args);
            _this.clear();
        }
        else
        {
            if (_this.runCounter > 0)
            {
                _this.runCounter--;
                _this.func(_this.args);
            }
            else if (_this.runCounter == -1)
            {
                _this.func(_this.args);
            }
            else
            {
                _this.clear();
            }
        }
    };
    if (this.runsOnce())
    {
        this.runCounter = 0;
        this.tID = setTimeout(_exec, this.ms);
    }
    else
    {
        this.runCounter = this.repeats;
        this.tID = setInterval(_exec, this.ms);
    }
}

EDIT: how I use it... 编辑:我如何使用它...

Task.tasks = {};
Task.exists = function(_ID)
{
    return (_ID in Task.tasks);
}
Task.create = function(_func, _ms, _repeats, _args, _ID)
{
    if (Task.exists(_ID)) return;
    Task.tasks[_ID] = new Task(_func, _ms, _repeats, _args);
}
Task.start = function(_ID)
{
    if (!Task.exists(_ID)) return;
    Task.tasks[_ID].start();
}
Task.clear = function(_ID)
{
    if (!Task.exists(_ID)) return;
    Task.tasks[_ID].clear();
}


//test task
__task = new Task(function(a){console.log( (new Date()).getTime()+': '+a ) }, 2000, 0, '[args]');

If you are getting that error in the .start() method, then that is because the this value is not what it is supposed to be. 如果在.start()方法中遇到该错误,那是因为this值不是应该的值。 And, that problem probably occurs because of the way you call the .start() method. 并且,该问题可能是由于调用.start()方法的方式而发生的。 Since you don't show us how you call the .start() method we can't help specifically on what is wrong there. 由于您没有向我们展示如何调用.start()方法,因此我们无法具体解决那里的问题。 But, you need to make sure that it is called with the form obj.start() where obj is a Task object. 但是,您需要确保以obj.start()的形式调用它,其中objTask对象。

A common mistake in this regard is to pass obj.start as a callback and not realize that it won't be called as obj.start() by whomever calls the callback. 在这方面的一个常见错误是将obj.start作为回调传递,而没有意识到无论调用该回调的人都不会将其称为obj.start()

Please include how you are calling .start() for us to help more specifically. 请包括您如何致电.start()以便我们更具体地提供帮助。

Your code is fine. 您的代码很好。 Make sure you're instatiating with new : 确保您使用了new

var task = new Task(...);
task.start();

Probably you're doing: 可能您正在做:

var task = Task(...);

that would not create a proper this value causing the error. 不会创建适当的this值,从而导致错误。

Cheers 干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道 var 的范围 - I don't know the scope of var 输入评论后出现关键错误。 我不知道如何解决(反应问题) - I got a key error after entering a comment. I don't know how to fix (react propblem) Javascript / CSS幻灯片:使用透明胶片突出了我不知道如何解决的缺陷 - Javascript/CSS slideshow: Using transparencies highlights flaws I don't know how to fix 我已经用JavaScript进行了幻灯片放映,但是我不知道如何使照片变得发呆 - I have made a slide show in javascript but I don't know how to make my pics faze 我不知道如何解决我的反向数组/字符串 - I don't know how to fix my reverse array/string 我不知道如何解决事件问题? - I don't know how to fix problem with events? 我不知道如何解决成绩计算器功能 - I don't know how to fix my grade calculator function 我不知道如何解决这个错误:Uncaught TypeError: Cannot read property 'classList' of undefined - I don't know how to fix this error: Uncaught TypeError: Cannot read property 'classList' of undefined 我收到此错误,但不知道如何解决,www.example.com/undefined?1460018627809 - I got this error but don't know how to fix it, www.example.com/undefined?1460018627809 我已经下载了syncfusion,我不知道如何运行syncfusion演示代码。 请任何人知道这件事。 发生此错误 - I have downloaded syncfusion, I don't know how to run syncfusion demo code . Please, anyone, know about it. this error occur
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM