Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
wait javascript

jj
Guest
Quote
2021-03-30 17:55:04

a easy one but how can i make a js script to wait eg 5 seconds?


nrasool
Registered User
Quote
2021-03-30 22:09:55

Hey

you should be able to use something like setTimeout in JS ie

setTimeout(function(){ alert("Hello"); }, 5000);


hadoken
Guest
Quote
2021-04-02 01:15:36

 setTimeout 
seems not to be available for Coppercube javascripting

Any other ideas?


hadoken
Guest
Quote
2021-04-02 01:33:30

I tested the following solution which seems to get the job for windows .exe target builds successfully done. Just follow the instructions from @xanimalkingx's first post (2019-03-08 22:10:29) at:

https://ambiera.com/forum.php?t=...


just_in_case
Moderator
Quote
2021-04-02 05:11:57

or you can simply check the action do something later which tells how you can store your current time on a key event or on executing the event and then use that time to compare each frame event with the new time. if the new time is greater than the old-time then do whatever you want. It is the same as setIntervalEvent() and it is not platform dependent it will work just fine for every platform as it uses the core javascript.

if you need more info on how to integrate this into your own script then let me know.


hadoken
Guest
Quote
2021-04-02 14:53:00

thanks @just_in_case for posting on the subject

How would you have to adjust the following example to use core javascript only without setTimeout? I assume by comparing Date/Time similar to calculating delta time and the mandatory use of some more code lines, loops & "ccbRegisterOnFrameEvent", right?

On keypress Coppercube shall do:


print("code before wait");

setTimeout(function(){ print("code after wait"); }, 3000);

print("more code without wait");



hadoken
Guest
Quote
2021-04-08 16:45:09

@just_in_case thanks for the timer programming help at https://ambiera.com/forum.php?t=...

with javascript execution after a "When a key is pressed" behavior I can report success with:

print("START");
var startTime = (new Date()).getTime();
var endTime = this.startTime + 1500;


function onFrameDrawing()
{
var now = (new Date()).getTime();
print(now);

if (now > endTime)
{
print("TIME UP!");
}
}

ccbRegisterOnFrameEvent(onFrameDrawing);

After putting the code example into an action it failed. I know there is already the official "action_DoLater.js" But I want to learn how to get this done from scratch. In the DoLater action the regular update of the timer is solved in a kind of nested way with more advanced function registering which I don't understand at the moment.

How could my attempt be corrected to make it also work as action? :

<action jsname="action_DoAfterWait" description="action_DoAfterWait">
</action>
*/

action_DoAfterWait = function()
{
}


action_DoAfterWait.prototype.execute = function(currentNode)
{
print("START");

var startTime = (new Date()).getTime();
var endTime = this.startTime + 1500;


function onFrameDrawing()
{
var now = (new Date()).getTime();
print(now);

if (now > endTime)
{
print("TIME UP!");
}
}

ccbRegisterOnFrameEvent(onFrameDrawing);
}



just_in_case
Moderator
Quote
2021-04-08 17:16:24

this should not work in the execute javascript action on "when a key is pressed do something behavior"

but it somehow works maybe coppercube execute javascript command uses javascript in a different manner than behavior and action.

The code you posted above contains an error so this should not work in either of the action or in the javascript execute command.

The whole code is correct except a silly mistake which you had done.

you have used "this.startTime" while in the whole script you haven't defined "this.startTime" anywhere, you have defined "var startTime" so you need to use "startTime" instead of "this.startTime". as "this.startTime" is not defined endTime will return a null or zero value and hence the timer never ends.
making the action unusable.

Just remove "this." from "this.startTime" and you are good to go.
Also, there is a mistake in the header of the action as well, but I believe this is because of copy-pasting the action into the forums. you will get an error of unterminated code or comment or something like that. because your action header is mission "/*" at the beginning of your action.

final code should be something like this.
/*<action jsname="action_DoAfterWait" description="action_DoAfterWait">
</action>
*/

action_DoAfterWait = function()
{
}


action_DoAfterWait.prototype.execute = function(currentNode)
{
print("START");

var startTime = (new Date()).getTime();
var endTime = startTime + 1500;


function onFrameDrawing()
{
var now = (new Date()).getTime();
print(now);

if (now > endTime)
{
print("TIME UP!");
}
}

ccbRegisterOnFrameEvent(onFrameDrawing);
}


hope that works


hadoken
Guest
Quote
2021-04-08 23:37:20

Sorry for my poor variable usage. The recent many working hours with Coppercube must have made me a bit unfocused on smaller but already understood details. Lots of copy & paste didn't make it better. But the good news is that your hope has come true - the code is working now and I have learned a lot again. Thank you @just_in_case!


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Int?rnational" (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