Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolov committed Apr 19, 2024
0 parents commit 89f517f
Show file tree
Hide file tree
Showing 15 changed files with 8,887 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .fatherrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// .fatherrc.js
export default {
// 以下为 esm 配置项启用时的默认值,有自定义需求时才需配置
esm: {
input: 'src', // 默认编译目录
platform: 'browser', // 默认构建为 Browser 环境的产物
transformer: 'babel', // 默认使用 babel 以提供更好的兼容性
},
// 以下为 cjs 配置项启用时的默认值,有自定义需求时才需配置
cjs: {
input: 'src', // 默认编译目录
platform: 'node', // 默认构建为 Node.js 环境的产物
transformer: 'esbuild', // 默认使用 esbuild 以获得更快的构建速度
},
umd: {
name: 'ctrlTimer',
entry: 'src/index', // 默认构建入口文件
},
};
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local
lib

coverage
.coveralls.yml
# Editor directories and files
.vscode
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.vercel
.env*.local
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- 16.19.0
env:
global:
- COVERALLS_PARALLEL=true
install:
- npm install
script:
- npm run coveralls
166 changes: 166 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@

# Enhanced and controllable setInterval、setTimeout in nodejs and browser
![GitHub commit activity](https://img.shields.io/github/commit-activity/t/dolov/ctrl-timer)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/ctrl-timer)
![NPM Version](https://img.shields.io/npm/v/ctrl-timer)
![NPM Type Definitions](https://img.shields.io/npm/types/ctrl-timer)
![NPM Downloads](https://img.shields.io/npm/d18m/ctrl-timer)
[![Coverage Status](https://coveralls.io/repos/github/Dolov/ctrl-timer/badge.svg?branch=main)](https://coveralls.io/github/Dolov/ctrl-timer?branch=main)



## Features
- Supporting pause and restart.
- The design of API is close to native API.
- Lightweight and does not rely on any third-party dependencies.
- Can clear or pause all instances simultaneously.

## Installation

```bash
npm i ctrl-timer
```

## Usage

### Interval

#### basic

```js

import { Interval } from 'ctrl-timer'

const timer = new Interval()

timer.setInterval(() => {
console.log('hello-world!')
}, 1000)

```

#### options
```js

import { Interval } from 'ctrl-timer'

const timer = new Interval({
// Max number of exec, default infinity
maxCount: 5,
// After max, clear or pause, default clear
maxClear: false,
})

timer.setInterval(() => {
console.log('hello-world!')
}, 1000)

```

#### pause restart
```js
import { Interval } from 'ctrl-timer'

const timer = new Interval()

timer.setInterval(() => {
console.log('hello-world!')
}, 1000)

timer.pause()
timer.restart()
```

#### update
```js

import { Interval } from 'ctrl-timer'

const timer = new Interval()

timer.setInterval(() => {
console.log('hello-world!')
}, 1000)

timer.update({
handler() {
console.log('new handler!')
},
timeout: 3000
})

```

#### clearInterval
```js

import { Interval } from 'ctrl-timer'

const timer = new Interval()

timer.setInterval(() => {
console.log('hello-world!')
}, 1000)

timer.clearInterval()
// not work
timer.pause()
// not work
timer.restart()
```

### Timeout
```js

import { Timeout } from 'ctrl-timer'

const timer = new Timeout()

timer.setTimeout(() => {
console.log('hello-world!')
}, 1000)

```

### Interval / Timeout static methods
```js

import { Interval } from 'ctrl-timer'

const timer = new Timeout()

timer.setTimeout(() => {
console.log('hello-world!')
}, 1000)

Interval.pause()
Interval.restart()
Interval.clearInterval()

```

## Interval Instance API
| methods | Description | Type | Default |
| ------------- | ------------------------- | ---------------------------- | ------- |
| setInterval | | | |
| pause | | | |
| restart | | | |
| update | update handler or timeout | ({handler, timeout}) => void | |
| clearInterval | clear | () => void | |

## Timeout Instance API
| methods | Description | Type | Default |
| ------------- | ------------------------- | ---------------------------- | ------- |
| setTimeout | | | |
| pause | | | |
| restart | | | |
| update | update handler or timeout | ({handler, timeout}) => void | |
| clearInterval | clear | () => void | |

## Class static methods
| methods | Description | Type | Default |
| ------------------------ | ----------- | ---------- | ------- |
| pause | pause all | () => void | |
| restart | restart all | () => void | |
| clearTimeout (Timeout) | clear all | () => void | |
| clearInterval (Interval) | clear all | () => void | |
11 changes: 11 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
// jest
presets: [
['@babel/preset-env', {
targets: {
node: 'current'
}
}],
'@babel/preset-typescript',
],
};
24 changes: 24 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

</body>
<script src="../dist/umd/ctrl-timer.min.js"></script>
<script>

const interval = new ctrlTimer.Interval()
const timeout = new ctrlTimer.Timeout()

timeout.setTimeout(() => {
console.log(12)
}, 1000);



</script>
</html>
45 changes: 45 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "ctrl-timer",
"version": "1.1.3",
"description": "Enhanced and controllable setInterval、setTimeout",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/Dolov/ctrl-timer.git"
},
"scripts": {
"start": "father dev",
"build": "father build",
"test": "jest",
"coverage": "jest --collectCoverage",
"coveralls": "jest --collectCoverage && cat coverage/lcov.info | coveralls -v",
"prepublishOnly": "npm run test && npm run build"
},
"keywords": [
"javascript timer",
"setTimeout",
"setInterval",
"pause",
"restart",
"control"
],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.24.3",
"@babel/preset-env": "^7.24.3",
"@babel/preset-typescript": "^7.24.1",
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.12",
"babel-jest": "^29.7.0",
"coveralls": "^3.1.1",
"father": "^4.4.0",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.4.3"
}
}

0 comments on commit 89f517f

Please sign in to comment.