'undefined' is not an object error when trying to use background service

You must Login before you can answer or comment on any questions.

Hi,

I am making a simple alarm clock. I found that the alarm stops when I use other apps on iPhone, so I would like to keep counting as a background service.

I simply added code as below, but I got an error message saying "Script Error = 'undefined' is not an object (evaluating 'Titanium.App.iOS') at ui.js (line 241)" Do you have tips on how to fix it ?

[on ui.js]

    var service = Titanium.App.iOS.registerBackgroundService({
        url:'bkcountdown.js'        
    });

[on bkcountdown.js] located in the same folder with ui.js

if(button.flag ="1"){
//button.flag=1 indicates that the count-down is on-going.
timer = setInterval(countdown, 1000);
}

1 Answer

did you do a clean of the project a force a complete application rebuild?

— answered 10 months ago by Aaron Saunders
answer permalink
4 Comments
  • Thank you for your advice. It started counting down !

    But I got another problem ..... After launching other apps, I tried going back to the original app. I got an error message as below: Application Error Could not find the file bkcountodown.js

    After I pressed Dismiss, I could see countdown was actually progressing, so obviously bkcoundtodwn.js exists and it background count-down is working. I have no idea why i am being told "Could not find the file bkcountdown.js".

    I would really appreciate one more piece of advice. Thank you !!

    — commented 10 months ago by Yuichi Kato

  • where is the sample code so maybe we can run the project and see if this is reproduceable

    — commented 10 months ago by Aaron Saunders

  • Thanks Aaron for your kind attention. Here is the code.

    [bkcountdown.js]

    if(button.flag ="1"){ timer = setInterval(countdown, 1000); }

    [ui.js]

    (function(){

    app.ui = {};
    
    var style = {
        barColor: '#000',
        backgroundColor: '#000'
    };
    
    
    app.ui.createApplicationTabGroup = function(){
        Titanium.UI.setBackgroundColor('#000');
    
        var tabGroup = Ti.UI.createTabGroup();
    
        var tab1 = app.ui.createStopWatchTab();
        var tab2 = app.ui.createHowToPlayTab();
    
        tabGroup.addTab(tab1);
        tabGroup.addTab(tab2);
    
        return tabGroup;
    };
    
    
    app.ui.createStopWatchTab = function(){
    
        var win = Titanium.UI.createWindow({
            title: 'Nap Timer',
            barColor: style.barColor,
            backgroundColor: style.backgroundColor
        });
        var tab = Titanium.UI.createTab({
            icon: 'dark_clock.png',
            title: 'Nap Timer',
            window: win
        });
    
    
        var label = Ti.UI.createLabel({
            text: '15:00',
            color: 'white',
            width: 'auto', height: 'auto', top: 30, 
            font: {fontSize: 30, fontWeight: 'bold'}
        });
        win.add(label);
    
        var button = Ti.UI.createButton({
            title: 'Start',
            backgroundImage:'blue_pill.gif',
            width: 150,
            height: 60,
            top: 120,
            color: 'white',
            font:{fontWeight:'bold',fontSize:30},
        });
        win.add(button);
    
        var button5min = Ti.UI.createButton({
            title: '5 Min',
            color: 'black',
            width: 70, height: 30,
            bottom: 60,
            left: 40,
            font:{fontWeight:'bold',fontSize:20},
        });
        win.add(button5min);
    
        var button10min = Ti.UI.createButton({
            title: '10 Min',
            color: 'black',
            width: 70, height: 30,
            bottom: 60,
            left: 120,
            font:{fontWeight:'bold',fontSize:20},
        });
        win.add(button10min);
    
        var button15min = Ti.UI.createButton({
            title: '15 Min',
            color: 'black',
            width: 70, height: 30,
            bottom: 60,
            left: 200,
            font:{fontWeight:'bold',fontSize:20},
        });
        win.add(button15min);
    
        var button30min = Ti.UI.createButton({
            title: '30 Min',
            color: 'black',
            width: 70, height: 30,
            bottom: 20,
            left: 40,
            font:{fontWeight:'bold',fontSize:20},
        });
        win.add(button30min);
    
        var button60min = Ti.UI.createButton({
            title: '60 Min',
            color: 'black',
            width: 70, height: 30,
            bottom: 20,
            left: 120,
            font:{fontWeight:'bold',fontSize:20},
        });
        win.add(button60min);
    
        var button90min = Ti.UI.createButton({
            title: '90 Min',
            color: 'black',
            width: 70, height: 30,
            bottom: 20,
            left: 200,
            font:{fontWeight:'bold',fontSize:20},
        });
        win.add(button90min);
    
    
        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";
                            button.flag="2";
                        }                   
        }
    
        function startcountdown(){
            timer = setInterval(countdown, 1000);
            button.title="Stop";
            button.flag="1";
        }
    
        function stopcountdown(){
            clearInterval(timer);
            button.title = "Start";
            button.flag="0";
        }
    
        function resetcountdown(){
            sound.stop();
            button.flag="0";
        }
    
    
        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;
    };
    
    
    app.ui.createHowToPlayTab = function(){
    
        var win = Titanium.UI.createWindow({
            title: 'Instruction',
            barColor: style.barColor,
            backgroundColor: style.backgroundColor
        });
        var tab = Titanium.UI.createTab({
            icon: 'dark_info.png',
            title: 'Instruction',
            window: win
        });
    
    
        var web = Ti.UI.createWebView({
            url: '/how_to_play.html'
        });
        win.add(web);
    
        return tab;
    }
    

    })();

    — commented 10 months ago by Yuichi Kato

  • Show 1 more comment

Your Answer

Think you can help? Login to answer this question!