【问题标题】:How to repeat part of this JS function forever every 30 seconds?如何每 30 秒永远重复这个 JS 函数的一部分?
【发布时间】:2016-11-22 15:33:18
【问题描述】:

好的,这就是我的函数的完整来源。我只希望被“///////////”包围的部分会重复。新功能也有效。我可以同时拥有它们,一旦我试图将突出显示的函数拉入一个新函数并出现大量错误时,我会感到非常困惑。

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));

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

    });  
}

【问题讨论】:

  • FetchConfirmations 你想要循环的还是里面的内容?

标签: javascript node.js steam


【解决方案1】:

使用计时器,间隔会很方便

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

【讨论】:

  • 最佳答案就在这里,伙计们^不知道为什么其他人会提供超出问题范围的不必要的复杂演示。通过可选的附加高级示例,答案应该是最低限度的。
  • @dylanh724 亲爱的“谎言创始人王座”很高兴在这里见到你。
【解决方案2】:

window.setInterval() 是你的朋友。 它在提供的时间间隔内执行一个功能。 例如,setInterval(()=>console.log("foo"),100) 将每 100 毫秒在控制台中记录一次“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)

【讨论】:

    【解决方案3】:

    将您的代码放入 setInterval(function(){},1000) 计时器或进行一些递归调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 2013-06-06
      相关资源
      最近更新 更多