gpt4 book ai didi

javascript - display.html typescript 不工作

转载 作者:行者123 更新时间:2023-12-01 02:09:11 24 4
gpt4 key购买 nike

我有定时器功能,可以倒计时。我将它从 js 移至 ts 代码

这是函数代码(TS)

  export module Timer {
export function startTimer(duration, display){
let session_timer;
Date.now = Date.now || (() => +new Date);
const start = Date.now();
let diff = 0;
let minutes:any = 0;
let seconds:any = 0;
const timer = function() {
diff = duration - (((Date.now() - start) / 1000) | 0);
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
if (minutes < 0) { minutes = "0"; }
if (seconds < 0) { seconds = "0"; }
minutes = minutes < 10 ? `0${minutes}` : minutes;
seconds = seconds < 10 ? `0${seconds}` : seconds;
display.html(`${minutes}:${seconds}`);
if (diff <= 0) {
$(this).changePage('#session_timer');
return clearInterval(session_timer);
}
};

timer();
return session_timer = setInterval(timer, 1000);
};

}


export default Timer;

我将它导入到index.ts,像这样的webpack组件

import Timer from "./scripts/timer";

导出默认定时器;

然后调用step2.js

像这样

 import Timer from "../components/timer";

$(document).ready(() => {
Timer.startTimer();
});

但是当我尝试运行项目时出现此错误

TypeError: Cannot read property 'html' of undefined

JS代码是这样的

 window.startTimer = function(duration, display) {
let session_timer;
Date.now = Date.now || (() => +new Date);
const start = Date.now();
let diff = 0;
let minutes = 0;
let seconds = 0;

const timer = function() {
diff = duration - (((Date.now() - start) / 1000) | 0);
minutes = (diff / 60) | 0;
seconds = (diff % 60) | 0;
if (minutes < 0) { minutes = "0"; }
if (seconds < 0) { seconds = "0"; }
minutes = minutes < 10 ? `0${minutes}` : minutes;
seconds = seconds < 10 ? `0${seconds}` : seconds;
display.html(`${minutes}:${seconds}`);
if (diff <= 0) {
$(this).changePage('#session_timer');
return clearInterval(session_timer);
}
};

timer();
return session_timer = setInterval(timer, 1000);
};

我该如何修复它?在 JS 中一切都很好

最佳答案

display未定义,因为您没有定义它...

您需要这样调用您的函数:

import Timer from "../components/timer";


let display = ;//whatever this is supposed to be?
//e.g. let display = $('.myClass');
//to pass in a html element of myClass.

$(document).ready(() => {
Timer.startTimer(100, display);
});

我假设这应该是一个 jQuery HTML 元素,这就是为什么你调用 display.html(...)

关于javascript - display.html typescript 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49875813/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com