首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何每30秒永远重复一部分JS功能?

如何每30秒永远重复一部分JS功能?
EN

Stack Overflow用户
提问于 2016-11-22 15:33:18
回答 3查看 239关注 0票数 0

好吧,这是我功能的全部来源。我只希望被“/”包围的部分重复一遍。新功能也能工作。当我试图将突出显示的函数拉到一个新的函数中,并得到大量错误时,我可能会把它们都弄糊涂了。

代码语言:javascript
复制
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));

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

    });  
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-11-22 15:35:44

使用计时器,间隔很方便

代码语言:javascript
复制
setInterval(function() {
    //.. part that should be repleated
}, 30*1000);
票数 1
EN

Stack Overflow用户

发布于 2016-11-22 15:35:59

window.setInterval()是你的朋友。它在规定的时间间隔内执行一个函数。例如,setInterval(()=>console.log("foo"),100)将每100 in在控制台中记录"foo“。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2016-11-22 15:36:00

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40745850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档