最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Run Loop In Background - Stack Overflow

programmeradmin8浏览0评论

So, I am needing to run an infinite loop in the background of my app (written in JS) that will be used to cycle a ScrollableView every six seconds. However, while this loop runs, I'm unable to preform any other operations in the app as you would think.

To sum up, how can I run this loop at all times while still making the app operational?

Code:

function startScrolling() {
    for(; ; ) {
        sleep(6000);
        Ti.API.info('Scrolling To Index: ' + viewIndex);
        scrollView.scrollToView(viewIndex);
        if(viewIndex == 4) {
            viewIndex = 0;
            scrollView.scrollToView(viewIndex);
        } else {
            scrollView.scrollToView(viewIndex);
            viewIndex++;
        }
    }
}

function sleep(milliseconds) {
    var start = new Date().getTime();
    while((new Date().getTime() - start) < milliseconds) {
        // Do nothing
    }
}

EDIT: Solution

setInterval(function() {
    Ti.API.info('Scrolling To Index: ' + viewIndex);
        scrollView.scrollToView(viewIndex);
        if(viewIndex == 4) {
            viewIndex = 0;
            scrollView.scrollToView(viewIndex);
        } else {
            scrollView.scrollToView(viewIndex);
            viewIndex++;
        }
}, 6000);

So, I am needing to run an infinite loop in the background of my app (written in JS) that will be used to cycle a ScrollableView every six seconds. However, while this loop runs, I'm unable to preform any other operations in the app as you would think.

To sum up, how can I run this loop at all times while still making the app operational?

Code:

function startScrolling() {
    for(; ; ) {
        sleep(6000);
        Ti.API.info('Scrolling To Index: ' + viewIndex);
        scrollView.scrollToView(viewIndex);
        if(viewIndex == 4) {
            viewIndex = 0;
            scrollView.scrollToView(viewIndex);
        } else {
            scrollView.scrollToView(viewIndex);
            viewIndex++;
        }
    }
}

function sleep(milliseconds) {
    var start = new Date().getTime();
    while((new Date().getTime() - start) < milliseconds) {
        // Do nothing
    }
}

EDIT: Solution

setInterval(function() {
    Ti.API.info('Scrolling To Index: ' + viewIndex);
        scrollView.scrollToView(viewIndex);
        if(viewIndex == 4) {
            viewIndex = 0;
            scrollView.scrollToView(viewIndex);
        } else {
            scrollView.scrollToView(viewIndex);
            viewIndex++;
        }
}, 6000);
Share Improve this question edited Jun 29, 2012 at 16:52 Jtaylorapps asked Jun 29, 2012 at 16:41 JtaylorappsJtaylorapps 5,7709 gold badges42 silver badges57 bronze badges 1
  • Javascript doesn't support threading so you'll want to use setInterval to run a function every X milliseconds. – sachleen Commented Jun 29, 2012 at 16:45
Add a comment  | 

1 Answer 1

Reset to default 16

Take a look at window.setInterval().

/* 
    Calls a function repeatedly, with a fixed 
    time delay between each call to that function.
*/
setInterval(startScrolling, 6000);
发布评论

评论列表(0)

  1. 暂无评论