简体   繁体   English

在该对象的另一个函数内部调用时,该对象的函数未被调用

[英]Function of an object not being called when being called inside another function of that object

I am still working on a timer and I'm rebuilding my code, so that the timer is an object, and so that I can have multiple.我仍在开发一个计时器,我正在重建我的代码,以便计时器是一个对象,这样我就可以拥有多个。 This is what I did so far:这是我到目前为止所做的:

$(document).ready(function() {
        a = new Timer("#timerText");
        a.set(9, 12);
    });

function Timer (element) {

var minutes, seconds, finalTimeInSeconds, displayMinutes, displaySeconds;

    this.set = function(inputMins, inputSecs) {
        alert("working!");
        minutes = inputMinutes; 
        seconds = inputSeconds; 
        finalTimeInSeconds = minutes*60 + seconds; //finalTimeInSeconds is the time it takes for the timer to be 00:00 in seconds.
        this.print();
    }

    this.print = function() {
        alert("printin!");
        displayMinutes = (minutes.toString().length == 2) ? minutes : "0" + minutes;    //ternary operator: adds a zero to the beggining 
        displaySeconds = (seconds.toString().length == 2) ? seconds : "0" + seconds;    //of the number if it has only one caracter.
        $(element).text(displayMinutes + ":" + displaySeconds);
    }

}

But the print function doesn't work when I call it via the set function.但是当我通过set函数调用它时, print函数不起作用。 Why?为什么?

Thanks to anyone that answers!感谢任何人的回答!

Your problem is the argument names — you're not using the names you've defined:您的问题是参数名称 - 您没有使用您定义的名称:

this.set = function(inputMins, inputSecs) {
    alert("working!");
    minutes = inputMins; // fix here
    seconds = inputSecs;  // fix here
    finalTimeInSeconds = minutes*60 + seconds; //finalTimeInSeconds is the time it takes for the timer to be 00:00 in seconds.
    this.print();
}

You must to learn to use the console.您必须学会使用控制台。 The error throws inmediately错误立即抛出

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM