Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
Posts: 26
Registered: ‎11-10-2013
My Device: Z10
My Carrier: Dialog

How do I use a "wait()" function in javascript with qml

I am importing an external javascript file to a qml fil, and then calling a function in the exrenal js. Now I need to set a timeout between each of the following instructions inside the javascript.

 

for example, 

 

function hello(){

dothis;
wait(2); // wait for 2 seconds
dothat;
wait(2); //wait for two seconds

.
.
.

}

 

 

Now how can I implement this wait() function? setTimeout function seems to work only in browsers.

Developer
Posts: 1,028
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: How do I use a "wait()" function in javascript with qml

[ Edited ]

I would probably use a QTimer, maybe something like this:

 

Page {
   id: page
   property int state
   attachedObjects: [
      QTimer {
         id: delay
         singleShot: true
         interval: 2000
         onTimeout: {
            switch (state) {
               case 1: {
                     doThat;
                     page.state = 2;
                     delay.start();
                  }
                  break;
               case 2: {
                     doMore;
                     page.state = 3;
                     delay.start();
                  }
                  break;
               case 3: {
                     doOneLastThing;
                  }
                  break;
            }
         }         
      }
   ]
   function hello() {
      page.state = 1;
      doThis;
      delay.start();
   }
}

This is just off the top of my head and given time I'm sure something more efficient or elegant could be devised.

 

EDIT: Oops, just noticed that you want your function to reside in an external .js file, and I'm not sure if my suggestion can be adapted to that. Of course, it would be easy if you are ok putting the QTimer and hello() function in your QML and then calling doThis, doThat, etc as external functions in your .js file.


fazlanzone7 wrote:

I am importing an external javascript file to a qml fil, and then calling a function in the exrenal js. Now I need to set a timeout between each of the following instructions inside the javascript.

 

for example, 

 

function hello(){

dothis;
wait(2); // wait for 2 seconds
dothat;
wait(2); //wait for two seconds

.
.
.

}

 

 

Now how can I implement this wait() function? setTimeout function seems to work only in browsers.






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
Contributor
Posts: 26
Registered: ‎11-10-2013
My Device: Z10
My Carrier: Dialog

Re: How do I use a "wait()" function in javascript with qml

I do not think, the solution you gave solves my issue. isn't there any other way to make a timeout in javascript?

Developer
Posts: 1,028
Registered: ‎03-20-2013
My Device: Red LE Developer Z10
My Carrier: Fido

Re: How do I use a "wait()" function in javascript with qml

I think perhaps you just do not understand what my code does or how it works. You wanted a solution that would execute some code, wait two seconds, execute some more code, wait two more seconds, execute yet more code... correct? Well that is exactly what the code I offered does.

 

I realize it is not as simple as just using a function like wait(), but sometimes you have to use the facilities available on a platform, not the ones you wish were there. Strictly speaking, code in a QML file is not JavaScript, but ECMAScript, which is similar, but not identical.

 

If you really want something you can use like wait() you could always roll one yourself. Perhaps something like:

 

Page {
	id: page
	attachedObjects: [
		QTimer {
			id: timer
			property bool done
			function wait(duration) {
				done = false;
				timer.interval = duration * 1000;
				start();
				while (!done) {
				}
			}
			onTimeout: {
				done = true;
			}
		}
	]
	function hello() {
		dothis;
		timer.wait(2); // wait for 2 seconds
		dothat;
		timer.wait(2); //wait for two seconds

		.
		.
		.

	}
}

But be aware that if you do it this way the application will be blocked for the duration of the wait and no other processing will happen in the background.


fazlanzone7 wrote:

I do not think, the solution you gave solves my issue. isn't there any other way to make a timeout in javascript?






Developer of Built for BlackBerry certified multiFEED RSS/Atom feed reader and aggregator.  multiFEED Icon

Play nice: Clicking Like Button on posts that helped you not only encourages others to continue sharing their experience, but also improves your own rating on this board. Also, don't forget to accept a post if it solves your problem or answers your question.
Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How do I use a "wait()" function in javascript with qml

I'm going to reiterate greenmr's point as I think it's important, implementing this code will make your app pretty much unresponsive to the user.

 

If you let us know what it is you are trying to do (i.e. what would the doThis() and doThat() functions be doing?) we can maybe give you what you need not what you are asking for. :smileyhappy:


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Contributor
Posts: 26
Registered: ‎11-10-2013
My Device: Z10
My Carrier: Dialog

Re: How do I use a "wait()" function in javascript with qml

okay, I need to run some javascript code once all the UI and backend code(such as connecting signals to the slots) has finished running.

 

If i call the js functions "onCreationcomplete" of the Page element, the javascript code executes at the following point.

 

AbstractPane *root = qml->createRootObject<AbstractPane>();

 at this point the rest of the code has not been executed. Therefore I need some way to delay my javascript execution point. one possible way is to change the event which it gets executed. But I need the code to get executed automatically, and I could not find a decent event for that.

 

I tried delaying the js execution events by implementing a custom delay function in js. but even then since my code gets executed in oncreationcomplete event, my goals were not met.

 

what is the possible solution to meet my goal?

 

 

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: How do I use a "wait()" function in javascript with qml

Not knowing your code I can't guarantee this will work but try setting your onCreationCompleted slot in the control that needs to be completed and then do the call or identify the last control that will get created and call from there.

 

For example if you have a pushed page you could call it in a pushTransitionEnded slot or if you have a ListView call it when the data has loaded.

 


If you've been helped click on Like Button, if you've been saved buy the app. :smileyhappy:

Developer of stokLocker, Sympatico and Super Sentences.
Contributor
Posts: 26
Registered: ‎11-10-2013
My Device: Z10
My Carrier: Dialog

Re: How do I use a "wait()" function in javascript with qml

I am unable to use the Qtimer object in QML. I get the following error.

 

asset:///main.qml:18:1: module "my.library" is not installed

 

This is how i imported and registered the timer.

 

import my.library 1.0 // in main.qml
qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer"); // main.cpp

 

 

qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer");
Contributor
Posts: 26
Registered: ‎11-10-2013
My Device: Z10
My Carrier: Dialog

Re: How do I use a "wait()" function in javascript with qml

I am unable to use the Qtimer object in QML. I get the following error.

 

asset:///main.qml:18:1: module "my.library" is not installed

 

This is how i imported and registered the timer.

 

import my.library 1.0 // in main.qml
qmlRegisterType<QTimer>("my.library", 1, 0, "QTimer"); // main.cpp

 

Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: How do I use a "wait()" function in javascript with qml

Looks correct, but you probably did something else wrong. slashkyle wrote a Timer class using QTimer a while ago

 

http://supportforums.blackberry.com/t5/Native-Development/QTimer/m-p/2550331#M45185

 

maybe that helps you to import QTimer into your QML

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o