#3947 Simulate 'setTimeout' and 'setInterval' in JSB.

This commit is contained in:
xingsenma 2014-02-13 13:54:05 +08:00
parent 508fcce64d
commit 4eecc7e56b
1 changed files with 60 additions and 0 deletions

View File

@ -3,6 +3,7 @@
//
var cc = cc || {};
var window = window || this;
cc.TARGET_PLATFORM = {
WINDOWS:0,
@ -821,3 +822,62 @@ cc.VisibleRect = {
}
};
var _windowTimeIntervalId = 0;
var _windowTimeFunHash = {};
var WindowTimeFun = cc.Class.extend({
_code: null,
_intervalId: 0,
ctor: function (code) {
this._intervalId = _windowTimeIntervalId++;
this._code = code;
},
fun: function () {
if (!this._code) return;
var code = this._code;
if (typeof code == "string") {
Function(code)();
}
else if (typeof code == "function") {
code();
}
}
});
/**
* overwrite window's setTimeout
@param {String|Function} code
@param {number} delay
@return {number}
*/
var setTimeout = function (code, delay) {
var target = new WindowTimeFun(code);
cc.Director.getInstance().getScheduler().scheduleCallbackForTarget(target, target.fun, delay / 1000, 0, 0, false);
_windowTimeFunHash[target._intervalId] = target;
return target._intervalId;
};
/**
* overwrite window's setInterval
@param {String|Function} code
@param {number} delay
@return {number}
*/
var setInterval = function (code, delay) {
var target = new WindowTimeFun(code);
cc.Director.getInstance().getScheduler().scheduleCallbackForTarget(target, target.fun, delay / 1000, cc.REPEAT_FOREVER, 0, false);
_windowTimeFunHash[target._intervalId] = target;
return target._intervalId;
};
/**
* overwrite window's clearInterval
@param {number} intervalId
*/
var clearInterval = function (intervalId) {
var target = _windowTimeFunHash[intervalId];
if (target) {
cc.Director.getInstance().getScheduler().unscheduleCallbackForTarget(target, target.fun);
delete _windowTimeFunHash[intervalId];
}
};
var clearTimeout = clearInterval;