├── .gitignore ├── LICENSE ├── README.md ├── component.json ├── nextTick.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .DS_Store 3 | node_modules/ 4 | test/test.html -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # next-tick 2 | 把一些异步操作整合到下一个CPU时间片执行,比多个异步操作多次调用setTimout(fn,0)的效率要高 3 | -------------------------------------------------------------------------------- /component.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nextTick", 3 | "version": "0.0.1", 4 | "main": "nextTick.js", 5 | "description": "把一些异步操作整合到下一个CPU时间片执行,比多个异步操作多次调用setTimout(fn,0)的效率要高" 6 | } -------------------------------------------------------------------------------- /nextTick.js: -------------------------------------------------------------------------------- 1 | /** 2 | * 将函数延迟执行 3 | * 4 | * @param {Function} fn 希望被延迟执行的函数 5 | * @param {Object} context 执行函数时上下文环境 6 | * @param {...} args 执行调用多个参数 7 | * @access public 8 | * @return {Number} 等待执行的任务数 9 | * 10 | * @example 11 | * 12 | * var fn = function(){ 13 | * console.log(2); 14 | * }; 15 | * 16 | * nextTick(fn); 17 | * console.log(1); 18 | * 19 | * // 1 20 | * // 2 21 | */ 22 | 'use strict'; 23 | 24 | 25 | (function (global, undefined) { 26 | 27 | var callbacks = []; //等待调用的函数栈 28 | var running = false; //当前是否正在运行中 29 | var slice = [].slice; 30 | var setImmediate = global.setImmediate || function (fn) { 31 | return global.setTimeout(fn, 0); 32 | }; 33 | 34 | //调用所有在函数栈中的函数 35 | //如果在执行某函数时又有新的函数被添加进来, 36 | //该函数也会在本次调用的最后被执行 37 | function callAllCallbacks() { 38 | var cbs = callbacks; 39 | callbacks = []; 40 | running = false; 41 | 42 | var count = cbs.length; 43 | for (var index = 0; index < count; index++) { 44 | var callback = cbs[index]; 45 | var fn = callback[0]; 46 | var context = callback[1]; 47 | fn.apply(context, callback.slice(2)); 48 | } 49 | } 50 | 51 | function nextTick(fn, context, args) { 52 | var callback = slice.call(arguments); 53 | 54 | //将函数存放到待调用栈中 55 | callbacks.push(callback); 56 | 57 | //判断定时器是否启动 58 | //如果没有启动,则启动计时器 59 | //如果已经启动,则不需要做什么 60 | //本次添加的函数会在 callAllCallbacks 时被调用 61 | if (!running) { 62 | running = true; 63 | setImmediate(callAllCallbacks, 0); 64 | } 65 | 66 | return callbacks.length; 67 | } 68 | 69 | if (typeof module === 'object' && module && typeof module.exports === 'object') { 70 | module.exports = nextTick; 71 | } else if (typeof define === 'function' && define.amd) { 72 | define('nextTick', [], function () { 73 | return nextTick; 74 | }); 75 | } else { 76 | global.nextTick = nextTick; 77 | } 78 | 79 | })(typeof global === 'object' ? global : window); 80 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "next-tick", 3 | "version": "0.0.1", 4 | "description": "把一些异步操作整合到下一个CPU时间片执行,比多个异步操作多次调用setTimout(fn,0)的效率要高", 5 | "scripts": { 6 | "test": "mocha test" 7 | }, 8 | "keywords": [ 9 | "setTimeout", 10 | "setImmediate" 11 | ], 12 | "author": "ustbhuangyi", 13 | "license": "MIT", 14 | "devDependencies": { 15 | "chai": "~1.8.1", 16 | "mocha": "~1.17.0" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "https://github.com/DDFE/next-tick" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by huangyi on 15/11/20. 3 | */ 4 | 5 | var expect = require('chai').expect; 6 | var nextTick = require('../nextTick'); 7 | 8 | describe('next-tick', function () { 9 | it('callback length should equal to the nextTick called time', function (done) { 10 | var fn1 = function () { 11 | console.log(1); 12 | }; 13 | 14 | expect(nextTick(fn1)).to.equal(1); 15 | 16 | function A() { 17 | this.hehe = 'hehe'; 18 | } 19 | 20 | var fn2 = function () { 21 | console.log(this.hehe); 22 | }; 23 | 24 | var a = new A(); 25 | 26 | expect(nextTick(fn2, a)).to.equal(2); 27 | 28 | var fn3 = function (a, b) { 29 | console.log(a); 30 | console.log(b); 31 | }; 32 | 33 | expect(nextTick(fn3, null, 'a', 'b')).to.equal(3); 34 | 35 | var fn4 = function () { 36 | 37 | expect(nextTick(function () { 38 | console.log(5); 39 | })).to.equal(1); 40 | 41 | console.log(4); 42 | 43 | done(); 44 | }; 45 | 46 | expect(nextTick(fn4)).to.equal(4); 47 | }); 48 | }); --------------------------------------------------------------------------------