2016-11-22 111 views
0

好的,所以继承我的功能的全部来源。我只希望那些被“////////////”包围的部分会重复。新功能也起作用。我可以让他们两个都很困惑,一旦我试图将突出显示的功能拖入新的功能并且出现大量错误。如何每30秒钟重复一次这个JS函数?

function reWebLogOn(steam, callback) { 
    steam.webLogOn(function(newCookie){ 
     helper.msg('webLogOn ok'); 
     cookies = newCookie; 
     offers.setup({ 
      sessionID: currentSessionId, 
      webCookie: newCookie 
     }, function(){ 
      if (typeof callback == "function") { 
       callback(); 
      } 
     }); 
     var steamcommunityMobileConfirmations = new SteamcommunityMobileConfirmations(
    { 
     steamid:   config.steamid, 
     identity_secret: config.identitySecret, 
     device_id:  device_id, 
     webCookie:  newCookie, 
    }); 


/////////////////////////////////////////////////////////////////////////////////////////////////////////// 


    steamcommunityMobileConfirmations.FetchConfirmations((function (err, confirmations) 
{ 
    if (err) 
    { 
     console.log(err); 
     return; 
    } 
    console.log('steamcommunityMobileConfirmations.FetchConfirmations received ' + confirmations.length + ' confirmations'); 
    if (! confirmations.length) 
    { 
     return; 
    } 
    steamcommunityMobileConfirmations.AcceptConfirmation(confirmations[0], (function (err, result) 
    { 
     if (err) 
     { 
      console.log(err); 
      return; 
     } 
     console.log('steamcommunityMobileConfirmations.AcceptConfirmation result: ' + result); 
    }).bind(this)); 
}).bind(this)); 

/////////////////////////////////////////////////////////////////////////////////////////////////////////// 

    }); 
} 
+0

是'FetchConfirmations'你想要什么或者它里面的内容? – Jacksonkr

回答

0

使用定时器,区间将方便

setInterval(function() { 
    //.. part that should be repleated 
}, 30*1000); 
+0

已添加,它的工作,非常感谢你! – Petras

+0

@Petras我欢迎:) –

0

window.setInterval()是你的朋友。它在所提供的时间间隔执行一个功能。例如, ,setInterval(()=>console.log("foo"),100)将每隔100ms在控制台中记录“foo”。

function reWebLogOn(steam, callback) { 
    steam.webLogOn(function(newCookie){ 
     helper.msg('webLogOn ok'); 
     cookies = newCookie; 
     offers.setup({ 
      sessionID: currentSessionId, 
      webCookie: newCookie 
     }, function(){ 
      if (typeof callback == "function") { 
       callback(); 
      } 
     }); 
     var steamcommunityMobileConfirmations = new SteamcommunityMobileConfirmations(
    { 
     steamid:   config.steamid, 
     identity_secret: config.identitySecret, 
     device_id:  device_id, 
     webCookie:  newCookie, 
    }); 


/////////////////////////////////////////////////////////////////////////////////////////////////////////// 

setInterval((function(){ 
    steamcommunityMobileConfirmations.FetchConfirmations((function (err, confirmations){ 
    if (err) 
    { 
     console.log(err); 
     return; 
    } 
    console.log('steamcommunityMobileConfirmations.FetchConfirmations received ' + confirmations.length + ' confirmations'); 
    if (! confirmations.length) 
    { 
     return; 
    } 
    steamcommunityMobileConfirmations.AcceptConfirmation(confirmations[0], (function (err, result) 
    { 
     if (err) 
     { 
      console.log(err); 
      return; 
     } 
     console.log('steamcommunityMobileConfirmations.AcceptConfirmation result: ' + result); 
    }).bind(this)); 
    }).bind(this)); 
}).bind(this),30000) 
0

把你的代码中的setInterval(函数(){},1000),定时器或做一些递归调用。