Is there anyway to loop vibration ? I looked around API doc but could not find it. Then I tried to make a looping logic as below. But it did not work and ended up with infinite loop.... I would appreciate your advice on how to enable loop vibration.
looping part
do{ Titanium.Media.vibrate(); }while(button.flag=="2"); button.flag = "0" button.addEventListener('click', function(){ if(button.flag=="1"){ stopcountdown(); } else if(button.flag=="0"){ startcountdown(); button.title = "Stop"; } else if(button.flag=="2"){ resetcountdown(); label.text = "Select timer"; button.title = "Stopped"; }
entire code
button5min.addEventListener('click', function(){ usersetTime = 5 * 60; label.text ="5:00"; durationTime = 0; //????????,????0 currentTime = usersetTime - durationTime; button.title = "Start"; }) button10min.addEventListener('click', function(){ usersetTime = 10 * 60; label.text ="10:00"; durationTime = 0; //????????,????0 currentTime = usersetTime - durationTime; button.title = "Start"; }) button15min.addEventListener('click', function(){ usersetTime = 15 * 60; label.text ="15:00"; durationTime = 0; //????????,????0 currentTime = usersetTime - durationTime; button.title = "Start"; }) button30min.addEventListener('click', function(){ usersetTime = 30 * 60; label.text ="30:00"; durationTime = 0; //????????,????0 currentTime = usersetTime - durationTime; button.title = "Start"; }) button60min.addEventListener('click', function(){ usersetTime = 60 * 60; label.text ="60:00"; durationTime = 0; //????????,????0 currentTime = usersetTime - durationTime; button.title = "Start"; }) button90min.addEventListener('click', function(){ usersetTime = 90 * 60; label.text ="90:00"; button.title = "Start"; durationTime = 0; //????????,????0 currentTime = usersetTime - durationTime; var button; button.title = "Start"; }) var setminute = 0.1; var usersetTime = setminute * 60; var durationTime = 0; var currentTime = usersetTime - durationTime; var timer; var sound = Ti.Media.createSound({url:"el_alarm.mp3"}); function countdown(){ if(currentTime>0){ durationTime = durationTime + 1; currentTime = usersetTime - durationTime; var minute = Math.floor(currentTime/60); var sec = Math.floor(currentTime - minute*60); label.text = minute + ":" + ("0" + sec).slice(-2); } else{ stopcountdown(); sound.looping = true; sound.play(); label.text = "Wake up !"; button.title="OK"; do{ Titanium.Media.vibrate(); }while(button.flag=="2"); } } function startcountdown(){ timer = setInterval(countdown, 1000); button.title="Stop"; button.flag="1"; } function stopcountdown(){ clearInterval(timer); button.title = "Start"; button.flag="2"; } function resetcountdown(){ sound.stop(); button.flag="0"; } // flag 0=??????????1=????????, 2=??????? button.flag = "0" button.addEventListener('click', function(){ if(button.flag=="1"){ stopcountdown(); } else if(button.flag=="0"){ startcountdown(); button.title = "Stop"; } else if(button.flag=="2"){ resetcountdown(); label.text = "Select timer"; button.title = "Stopped"; } }); var service = Titanium.App.iOS.registerBackgroundService({ url:'bkcountdown.js' }); return tab; };
1 Answer
in a Loop a condition needs to Change to have Chance that it will end sometime. Your condition is never changed (flag==2) therefore you have infinite Loop. You may try some sort of recursive function using setTimeout or setInterval.
Your Answer
Think you can help? Login to answer this question!