(function (global, factory) { if (typeof exports === 'object') { module.exports = factory(); } else if (typeof define === 'function' && define.amd) { define('PublicInterval', factory); } else { global.PublicInterval = factory(); } })(this, function () { function PublicInterval (option) { option = option || {} this.timer = option.timer || 16.7 this.processor = option.processor || {} this.flag = typeof option.autorun === 'bool' ? option.autorun : false this.isRunning = false this.handler = function () { if (this.flag) { for (key in this.processor) { setTimeout(this.processor[key], 0) } setTimeout(this.handler.bind(this), this.timer) } } option.autorun && this.go() } PublicInterval.prototype.go = function () { this.flag = true !this.isRunning && this.handler() } PublicInterval.prototype.stop = function () { this.isRunning = false this.flag = false } PublicInterval.prototype.removeProcessor = function (key) { delete(this.processor[key]) } PublicInterval.prototype.setProcessor = function (key, fn) { if (typeof fn === 'function') { this.processor[key] = fn } else { throw('This processor is not a function.') } } PublicInterval.prototype.setTimer = function (time) { this.timer = +time } return PublicInterval }) stage = new createjs.Stage("mycanvas"); //创建一个形状的显示对象 circle = new createjs.Shape(); circle.graphics.beginFill("red").drawCircle(0, 0, 40); //形状实例的设置位置 circle.x = circle.y = 50; //添加形状实例到舞台显示列表 stage.addChild(circle); //更新阶段将呈现下一帧 stage.update(); circle.addEventListener("click", handleClick); function handleClick(event){ // 鼠标点击事件 circle.x += 10 circle.y += 10 stage.update() console.log('click') } document.body.onkeydown = function (event) { // 鼠标点击事件' console.log('keydown') if(event.keyCode === 37) { circle.x -= 10 } else if (event.keyCode ===39) { circle.x += 10 } stage.update() }