Hi all,
I'm a n00b at JS so I apologise for the level of this question but I just can't figure this out?!
Can anyone please explain to me WHY this fails?
I know people prefer to use self calling functions etc but I just can't get this code to execute correctly. (I also know I am using global variables and other bad practices but I have been trying to write a different program and it is the while or do-while loops that keep causing execution issues so I have written this, reduced to its most basic level, just as a code testbed)
I am not looking for the way I ought to do it per-se so much as understanding why / how JS works so I understand why this code doesn't!
Many thanks in advance!
This is running on an android emulator in Windows Vista (I know - ew!) Titanium Studio, build: 2.0.1.201204132053. Android 2.0.1GA2 Runtime V8
If the "while" loop is commented out, it runs and displays a blue window and the label "Done!"
WITH the while loop, all I get is the red Titanium banner page and it goes no further!
What don't I understand about while loops / JavaScript flow control / execution please?
var counter = 5;
var win = Ti.UI.createWindow({backgroundColor:'#00f'});
var label = Ti.UI.createLabel({ color:'#fff', top:(Ti.Platform.displayCaps.platformHeight)/2, });
win.add(label);
win.open();
while (counter>0);
{
label.text=counter;
counter-=1;
}
label.text = 'Done!';
5 Answers
Accepted Answer
I dont see any fail in the while loop, I tried to run your code in my android device and it worked after some changes. you have given a semicolon after the while syntax, remove that (it may be your typo mistake)
here is the code which I tried and it showed me the blue screen and the label done
var counter = 5; var win = Ti.UI.createWindow({backgroundColor:'#00f'}); var label = Ti.UI.createLabel({ color:'#fff', top:(Ti.Platform.displayCaps.platformHeight)/2,height:50, width: 100 }); win.add(label); win.open(); while (counter>0) { label.text=counter; counter-=1; } label.text = 'Done!';
Hm I dont see an error, try this:
for (var i=counter;i>0;i--) { label.setText(i); } label.setText('DONE');
Thanks for your response!
I see why that would work...
But why does the while loop fail?!
Thank you both so much!
I can't believe I missed something as obvious and simple as that!
I thought I was going mad! lol
Thanks again!
Thanks for your help so far guys,
There is definitely something I don't understand about program flow in Titanium / JS...
My above typo not withstanding, when I then took the corrected version to my application, it still fails in a way I don't expect.
Again, here is a simplified version that behaves 'oddly'.
I would have expected it to display an incredibly fast moving number that counts up to 2000.
Instead, it 'appears' to do nothing (sitting on the Appcelerator banner) while counting in the background and then only displays a window with "Done!" at the very end.
Can someone please explain to me why it displays nothing until the end of the program or the loop?
Many thanks again!
(ps - again - watch out for the "less-than" sign in the IF statement!)
var counter = 0;
var win = Ti.UI.createWindow({backgroundColor:'#00f'});
var label = Ti.UI.createLabel({ color:'#fff', text:'<blank>', top:(Ti.Platform.displayCaps.platformHeight)/2, });
win.add(label);
win.open();
do
{
counter+=1;
label.text=counter;
} while (counter<2000);
label.text = 'Done!';
Your Answer
Think you can help? Login to answer this question!