weixin_33688840 2017-02-08 20:15 采纳率: 0%
浏览 30


I'd like to create a live feed for the front page of Reddit by the use of the API. Since it has very limited support for Websocket, I thought I'd use a sub-optimal solution: my program would get the most recent posts with a certain frequency and append some data of them to an HTML table.

To provide some code, here's a shortened version of what I'm doing:

//cliendID and clientSec are declared vars I omit for safety reasons
function Auth(callback){

  //Authorization to Reddit API
      type: 'POST',
      url: "https://www.reddit.com/api/v1/access_token",

      beforeSend: function(xhr) {
               "Basic " + btoa(clientID + ":" + clientSec));

          grant_type: "client_credentials",
          user: clientID,
          password: clientSec

      success: callback

//params.listing and APIurl are also declared
function afterAuth(token){
    accessAPI(token, printResult, params.listing, APIurl);

//standardized Reddit API access
function accessAPI(response, callback, inputJSON, url){

  //console.log("accessAPI: "+response);

      type: 'GET',
      url: /* an oauth.reddit.com link */,

      beforeSend: function(xhr) {
        xhr.setRequestHeader("Authorization", response.token_type + " " + response.access_token);

      data: /* input necessary for that API request */

      success: callback


//test function, for demonstration purposes
function printResult(querydat){

What my program does is a simple call: Auth(afterAuth), where the chain follows:

  • The POST AJAX request within Auth, if successful, calls afterAuth.

  • afterAuth uses an identification token (if I remember correctly) as an input and functions as a "wrapper" to be able to run multiple API accesses.

  • In this example, accessAPI is used for a GET AJAX request which directly accesses an oauth.reddit.com link, from which it fetch subreddit data.

  • I manipulate this data through its success callback, which, here, is a simple demo function.

What I want to achieve is the periodic call of accessAPI so that it runs its callback on more recent posts as time goes on.

Because of the asynchronous nature of these calls (I presume), neither setInterval nor looped setTimeout seems to be a viable solution.

What else can I do to produce this periodic call?

(needless to say, repeated call of Auth() is impractical, but I can't see, how can I manipulate the inner flow otherwise.)

  • 写回答



      相关推荐 更多相似问题


      • ¥15 Android URL如何转成视频/音频,可行吗?
      • ¥20 SQL数据查询,子查询
      • ¥15 c++字符串分割问题
      • ¥15 关于#sql#的问题:没有用命令关闭cdc,手动把系统表开启的cdc右键删除了
      • ¥15 vue+uniapp
      • ¥15 android freedom
      • ¥15 使用自定义的类型代替内置类型可行吗
      • ¥15 关于STM32的SPI和ENDAT接口编码器通信的问题
      • ¥15 关于#pdfbox#生成的PDF文件正常,转图片中文乱码的问题,如何解决?
      • ¥15 ADS中有关DAC控件的使用问题