Skip to content

Commit

Permalink
1. 新增:每次调用渲染的间隔时间(ms)
Browse files Browse the repository at this point in the history
2. 所有时间都从 秒 改为 毫秒
  • Loading branch information
geofryzheng committed Apr 13, 2023
1 parent 0c2325f commit 75e332b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
6 changes: 2 additions & 4 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<script src="//unpkg.com/babel-standalone@6/babel.min.js"></script>

<script src="//unpkg.com/react-time-countdown"></script>
<!-- <script src="../dist/react-time-countdown.umd.production.min.js"></script> -->
<!--<script src="../dist/react-time-countdown.umd.production.min.js"></script>-->

<script type="text/babel">
const TimeCountdown = window['react-time-countdown'].default;
Expand All @@ -23,7 +23,6 @@
return (
<div>
<h3>倒计时</h3>

<TimeCountdown
deadlineTimestamp={deadline}
callback={() => {
Expand All @@ -45,9 +44,8 @@ <h3>倒计时</h3>
}}
intervalDelay={1000}
/>

毫秒
<hr />

<div>
设置结束时间戳:
<div
Expand Down
20 changes: 8 additions & 12 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useRef, useState } from 'react';
import { SET_INTERVAL, GET_REST_TIME } from './utils';
import React, { useEffect, useRef, useState } from "react";
import { SET_INTERVAL, GET_REST_TIME } from "./utils";

interface PropsType {
deadlineTimestamp: number; // 结束时间戳
Expand All @@ -12,20 +12,20 @@ interface PropsType {
intervalDelay?: number; // 每次调用渲染的间隔时间(毫秒,默认:1000)
}

export const Thing = function (props: PropsType) {
export const Thing = function(props: PropsType) {
const {
deadlineTimestamp,
leftMillisecond: _leftMillisecond = 0,
callback,
renderMillisecond,
intervalDelay = 1000,
intervalDelay = 1000
} = props;

const leftMillisecond = Math.max(_leftMillisecond, 0);

// 每秒执行(实例)
const setIntervalInstance = useRef<SET_INTERVAL | { stop: Function }>({
stop: () => {},
const setIntervalInstance = useRef<SET_INTERVAL>({
stop: () => {}
});

useEffect(() => {
Expand Down Expand Up @@ -53,13 +53,9 @@ export const Thing = function (props: PropsType) {
};
}, [deadlineTimestamp, leftMillisecond, callback, intervalDelay]);

const [restTotalSecond, setRestTotalSecond] = useState<number>(
GET_REST_TIME(deadlineTimestamp)
);
const [restTotalSecond, setRestTotalSecond] = useState<number>(GET_REST_TIME(deadlineTimestamp));

return renderMillisecond
? renderMillisecond(restTotalSecond)
: restTotalSecond;
return <>{renderMillisecond ? renderMillisecond(restTotalSecond) : restTotalSecond}</>;
};

export default Thing;
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class SET_INTERVAL {

constructor(func: Function, millisecond: number) {
let setIntervalId: ReturnType<typeof setTimeout>;
if (typeof func === 'function') {
if (typeof func === "function") {
setIntervalId = setTimeout(function self() {
setIntervalId = setTimeout(self, millisecond);
func();
Expand Down

0 comments on commit 75e332b

Please sign in to comment.