简体   繁体   中英

JavaScript setInterval - function only called once

I'm trying to retrieve twitter feeds periodically using setInterval calling a dajaxice function but I'm having problems getting the function to be called more than once. setInterval works if I do something like just write a line to the console but when I try to call my dajaxice function it only happens once.

ajax.py:

@dajaxice_register
def get_home_timeline(request):
    home_timeline = oauth_req(
    'http://api.twitter.com/1/statuses/home_timeline.json?count=2',
    settings.TWITTER_TOKEN_KEY,
    settings.TWITTER_TOKEN_SECRET
    )
    return simplejson.dumps({'home_timeline': home_timeline })

def oauth_req(url, key, secret, http_method="GET", post_body=None, http_headers=None):
    consumer = oauth.Consumer(key=settings.TWITTER_CONSUMER_KEY, secret=settings.TWITTER_CONSUMER_SECRET)token = oauth.Token(key=key, secret=secret)
client = oauth.Client(consumer, token)

resp, content = client.request(
url,
    method=http_method,
    # body=post_body,
    # headers=http_headers,
    # force_auth_header=True
)
return content

base.html:

var get_timeline = Dajaxice.modules.ticker.get_home_timeline(get_home_timeline_callback);

    function getTweets(){
        intervalId = setInterval(get_timeline, 2000);
    }

Thanks for any help,

Jason

var get_timeline = 
   Dajaxice.modules.ticker.get_home_timeline(get_home_timeline_callback);

That is not a closure to call the function, that immediately invokes the function (before you even set up the interval).

In think you want

var get_timeline = function(){
   Dajaxice.modules.ticker.get_home_timeline(get_home_timeline_callback);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM