Added setTimeout(), clearTimeout(), setInterval() and clearInterval() functions so javascript programmers familiar with these functions can use them in Minecraft

This commit is contained in:
walterhiggins 2013-10-09 18:42:39 +01:00
parent d3831bb910
commit 396a8e6107
2 changed files with 95 additions and 0 deletions

View file

@ -232,6 +232,38 @@ See [issue #69][issue69] for more information.
[issue69]: https://github.com/walterhiggins/ScriptCraft/issues/69 [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 Drone Module
============ ============
The Drone is a convenience class for building. It can be used for... The Drone is a convenience class for building. It can be used for...

View file

@ -698,7 +698,70 @@ See [issue #69][issue69] for more information.
global.command = _command; global.command = _command;
global._onTabComplete = __onTabCompleteJS; global._onTabComplete = __onTabCompleteJS;
global.addUnloadHandler = _addUnloadHandler; 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/ // assumes this was loaded from js-plugins/core/
// load all of the plugins. // load all of the plugins.