Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在计时器中调用lap函数_Javascript_Jquery_Timer - Fatal编程技术网

Javascript 在计时器中调用lap函数

Javascript 在计时器中调用lap函数,javascript,jquery,timer,Javascript,Jquery,Timer,我已经为这个问题烦恼了一段时间了,我有一个计时器,当我调用Stopwatch.start()时,它有4个按钮:开始、停止、重置和圈,秒表.stop()或秒表.reset()工作正常,但当我试图调用Stopwatch.lap()时它不工作 我以前有过答案,但现在我有了一台新电脑和类似的东西,我不能调用这个函数。控制台显示“未捕获类型错误:Stopwatch.lap不是函数”,我找不到问题 这是我的密码: var Stopwatch = { init: function(elem, options)

我已经为这个问题烦恼了一段时间了,我有一个计时器,当我调用
Stopwatch.start()时,它有4个按钮:开始、停止、重置和圈
秒表.stop()
秒表.reset()工作正常,但当我试图调用
Stopwatch.lap()时它不工作

我以前有过答案,但现在我有了一台新电脑和类似的东西,我不能调用这个函数。控制台显示“未捕获类型错误:Stopwatch.lap不是函数”,我找不到问题

这是我的密码:

var Stopwatch = {
init: function(elem, options) {

    var timer       = createTimer(),
            startButton = createButton("start", start),
        stopButton  = createButton("stop", stop),
        resetButton = createButton("reset", reset),
            lapButton   = createButton("lap", lap),
            lapSpan     = createTimer(),
        offset,
        clock,
        interval;

    options = options || {};
    options.delay = options.delay || 1;

    elem.appendChild(timer);
    elem.appendChild(startButton);
    elem.appendChild(stopButton);
    elem.appendChild(resetButton);
    elem.appendChild(lapButton);
    elem.appendChild(lapSpan);

    reset();

    function createTimer() {
        return document.createElement("span");
    }

    function createButton(action, handler) {
        var a = document.createElement("a");
        a.href = "#" + action;
        a.innerHTML = action;
        a.addEventListener("click", function(event) {
            handler();
            event.preventDefault();
        });
        return a;
    }

    function start() {
        if (!interval) {
            offset = Date.now();
            interval = setInterval(update, options.delay);
        }
    }

    function stop() {
        if (interval) {
            clearInterval(interval);
            interval = null;
        }
    }

    function lap() {
            lapSpan.innerHTML=timer.innerHTML;
        }

    function reset() {
        clock = 0;
        render(0);
                lap();
    }

    function update() {
        clock += delta();
        render();
    }

    function render() {
        timer.innerHTML = clock / 1000;
    }

    function delta() {
        var now = Date.now(),
        d = now - offset;

        offset = now;
        return d;
    }

        this.getTime=function() {
            return clock;
        }

    this.start = start;
    this.stop = stop;
    this.reset = reset;
}
};
var elems;
window.onload=function() {
   elems = document.getElementsByClassName("basic");
   for (var i = 0, len = elems.length; i < len; i++) {
       Stopwatch.init(elems[i]);
   }
}
在这个项目中,我最熟悉JavaScript和jQuery,但我也知道HTML和JavaScript。

您需要添加

this.lap = lap;
排队后

this.start = start;
this.stop = stop;
this.reset = reset;
您需要添加

this.lap = lap;
排队后

this.start = start;
this.stop = stop;
this.reset = reset;

您确实在类/对象中定义了函数,但忘记添加
this.lap=lap以便可以将其用作方法/属性

应该是:

this.start = start;
this.stop = stop;
this.reset = reset;
this.lap = lap; // this one was missing

您确实在类/对象中定义了函数,但忘记添加
this.lap=lap以便可以将其用作方法/属性

应该是:

this.start = start;
this.stop = stop;
this.reset = reset;
this.lap = lap; // this one was missing

我想您需要添加
this.lap=lap同样在该类/对象的结尾…天哪,非常感谢您,发布答案,我将其标记为答案!:DI supose您需要添加
this.lap=lap同样在该类/对象的结尾…天哪,非常感谢您,发布答案,我将其标记为答案!:D