type Milliseconds = number;
type Seconds = number;
type Timestamp = {
start: Milliseconds;
end: Milliseconds;
duration: Milliseconds;
};
type Timestamps = Record<string, Timestamp>;
let timestamps: Timestamps = {};
const record = (name) => {
if (timestamps[name]) {
return;
}
timestamps[name] = {
start: Date.now(),
end: 0,
duration: 0,
};
};
const over = (name) => {
if (!timestamps[name]) {
return;
}
const { start } = timestamps[name];
const end = Date.now();
const duration = end - start;
timestamps[name] = {
start,
end,
duration,
};
};
const calculateTotal = () => {
Object.keys(timestamps).reduce((total, currKey) => {
const { duration } = timestamps[currKey];
return (total += duration);
}, 0);
};
用法:
record('init')
over('init');
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。