【问题标题】:Function of an object not being called when being called inside another function of that object在该对象的另一个函数内调用时未调用该对象的函数
【发布时间】:2015-09-14 11:35:50
【问题描述】:

我仍在开发计时器,并且正在重新构建我的代码,以便计时器是一个对象,因此我可以拥有多个。这是我到目前为止所做的:

$(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);
    }

}

但是当我通过set 函数调用它时,print 函数不起作用。为什么?

感谢任何回答的人!

【问题讨论】:

  • 任何开发者工具控制台错误?
  • 定义“不起作用”。它有什么作用,它没有什么作用?
  • 这是一个非常幼稚的问题,但我很高兴我做到了,因为现在我知道了控制台 :-)
  • @JaromandaX 是的,我现在修好了

标签: javascript function object


【解决方案1】:

您的问题是参数名称 - 您没有使用您定义的名称:

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();
}

您必须学会使用控制台。错误立即抛出

【讨论】:

  • 当然,很抱歉我的聪明才智,但我要开始了。从现在开始我会检查控制台。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多