繁体   English   中英

如何每30秒重复一次此JS函数的一部分?

[英]How to repeat part of this JS function forever every 30 seconds?

好的,这就是我功能的全部来源。 我只想重复用“ ////////////”包围的部分。 新功能也可以使用。 当我尝试将突出显示的功能拖入新功能并出现大量错误时,我可能会感到非常困惑。

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

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

    });  
}

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

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

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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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