Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
variable time delay

Robo
Guest
Quote
2020-04-03 13:52:04

If you have ever needed the javascript code to run a variable amount of time delay this is it.

'wait' is the variable in ms containing the amount of delay required. You run the function with a simple "Delay(wait);"

// time delay
var Delay = function Wait(timeMs) {
var me = this;
this.registeredFunction = function() {
me.doLaterFuncM();
};
ccbRegisterOnFrameEvent(me.registeredFunction);
this.startTime = (new Date()).getTime();
this.endTime = this.startTime + timeMs;
}
function doLaterFuncM() {
var nowM = (new Date()).getTime();
if (nowM > this.endTime) {
...add code needed here to run when done
ccbUnregisterOnFrameEvent(this.registeredFunction);
return true;
}}
Delay(wait);
}


aloan
Registered User
Quote
2020-05-15 00:38:22

Lol I am trying to change a variable after 1 second, what's wrong here?
var timer = 0;

// time delay
var Delay = function Wait(1000) {
var me = this;
this.registeredFunction = function() {
me.doLaterFuncM();
};
ccbRegisterOnFrameEvent(me.registeredFunction);
this.startTime = (new Date()).getTime();
this.endTime = this.startTime + 1000;
}
function doLaterFuncM() {
var nowM = (new Date()).getTime();
if (nowM > this.endTime) {
ccbSetCopperCubeVariable("timer", 1);
ccbUnregisterOnFrameEvent(this.registeredFunction);
return true;
}}
Delay(2000);
}



Robo
Guest
Quote
2020-05-15 12:50:15

I would leave the start as is - " var Delay = function Wait(timeMs) {" -dont put a number in there.


aloan
Registered User
Quote
2020-05-15 22:22:37

oh I see, This is the downloadable do later editor script which I know and have and use. But I 'm looking for a solution outside the editor and into an external JS text file...


Robo
Guest
Quote
2021-04-09 05:03:04

You can create an extension with this:

// This is a scripted coppercube action.
// It does an action after variable 'delay' received else no delay
/*
<action jsname="action_DoLaterVar" description="Do later with variable delay">
<property name="ActionToDo" type="action" />
</action>
*/

action_DoLaterVar = function(){};

action_DoLaterVar.prototype.execute = function(currentNode) {
var me = this;
this.wait = ccbGetCopperCubeVariable("delay");
this.registeredFunction = function() {
me.variableWait();
};

ccbRegisterOnFrameEvent(me.registeredFunction);
this.startTime = (new Date()).getTime();
this.endTime = this.startTime-(-this.wait);
this.currentNode = currentNode;
}

action_DoLaterVar.prototype.variableWait = function() {
var now = (new Date()).getTime();
if (now > this.endTime) {
ccbInvokeAction(this.ActionToDo, this.currentNode);
ccbUnregisterOnFrameEvent(this.registeredFunction);
}}


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "I?ternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact