diff --git a/docs/api.md b/docs/api.md index 51d4caa..db37e32 100644 --- a/docs/api.md +++ b/docs/api.md @@ -232,6 +232,38 @@ See [issue #69][issue69] for more information. [issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 +setTimeout() function +--------------------- + +This function mimics the setTimeout() function used in browser-based javascript. +However, the function will only accept a function reference, not a string of javascript code. +Where setTimeout() in the browser returns a numeric value which can be subsequently passed to +clearTimeout(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearTimeout() implementation. + +If Node.js supports setTimeout() then it's probably good for ScriptCraft to support it too. + +[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html + +clearTimeout() function +--------------------- +A scriptcraft implementation of clearTimeout(). + +setInterval() function +--------------------- + +This function mimics the setInterval() function used in browser-based javascript. +However, the function will only accept a function reference, not a string of javascript code. +Where setInterval() in the browser returns a numeric value which can be subsequently passed to +clearInterval(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearInterval() implementation. + +If Node.js supports setInterval() then it's probably good for ScriptCraft to support it too. + +[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html + +clearInterval() function +--------------------- +A scriptcraft implementation of clearInterval(). + Drone Module ============ The Drone is a convenience class for building. It can be used for... diff --git a/src/main/javascript/core/_scriptcraft.js b/src/main/javascript/core/_scriptcraft.js index 68a213b..4739d1b 100644 --- a/src/main/javascript/core/_scriptcraft.js +++ b/src/main/javascript/core/_scriptcraft.js @@ -698,7 +698,70 @@ See [issue #69][issue69] for more information. global.command = _command; global._onTabComplete = __onTabCompleteJS; global.addUnloadHandler = _addUnloadHandler; + +/************************************************************************* +setTimeout() function +--------------------- + +This function mimics the setTimeout() function used in browser-based javascript. +However, the function will only accept a function reference, not a string of javascript code. +Where setTimeout() in the browser returns a numeric value which can be subsequently passed to +clearTimeout(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearTimeout() implementation. + +If Node.js supports setTimeout() then it's probably good for ScriptCraft to support it too. + +[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html + +***/ + global.setTimeout = function( callback, delayInMillis){ + // + // javascript programmers familiar with setTimeout know that it expects + // a delay in milliseconds. However, bukkit's scheduler expects a delay in ticks + // (where 1 tick = 1/20th second) + // + var bukkitTask = server.scheduler.runTaskLater(__plugin, callback, delayInMillis/50); + return bukkitTask; + }; +/************************************************************************* +clearTimeout() function +--------------------- +A scriptcraft implementation of clearTimeout(). + +***/ + global.clearTimeout = function(bukkitTask){ + bukkitTask.cancel(); + }; + +/************************************************************************* +setInterval() function +--------------------- + +This function mimics the setInterval() function used in browser-based javascript. +However, the function will only accept a function reference, not a string of javascript code. +Where setInterval() in the browser returns a numeric value which can be subsequently passed to +clearInterval(), This implementation returns a [BukkitTask][btdoc] object which can be subsequently passed to ScriptCraft's own clearInterval() implementation. + +If Node.js supports setInterval() then it's probably good for ScriptCraft to support it too. + +[btdoc]: http://jd.bukkit.org/beta/apidocs/org/bukkit/scheduler/BukkitTask.html + +***/ + global.setInterval = function(callback, intervalInMillis){ + var delay = intervalInMillis/ 50; + var bukkitTask = server.scheduler.runTaskTimer(__plugin, callback, delay, delay); + return bukkitTask; + }; +/************************************************************************* +clearInterval() function +--------------------- +A scriptcraft implementation of clearInterval(). + +***/ + global.clearInterval = function(bukkitTask){ + bukkitTask.cancel(); + }; + // // assumes this was loaded from js-plugins/core/ // load all of the plugins.