最近编辑我的代码后,setInterval拒绝运行我的函数...为什么?

时间:2015-03-16 02:43:27

标签: javascript node.js setinterval

让你了解我在标题中的意思。

看一下setInterval停止工作之前的代码。

var anime = function(){
    _.each(db.get('','animedb'), function(site){
        var ann = function(){

^函数在var

for (var epid in eps) {
    epid = parseInt(epid, 10);
    var eptime = (new Date(eps[epid].pubDate[0])*1000)/1000;
    if(eptime > site.lastbuilddate){
        counter = counter+1;
        if(counter < 6){
            list.push(font(colors['normal'])+eps[epid].title[0] +' - ['+ utils.secondsToString((new Date() - (eptime+site.delay))/1000, 1)+' ago.]</f>');
        }
    }
};

^这是在编辑后打破所有内容的部分

var run = setInterval(ann, site.interval*60000);

^这里是setInterval,它位于每个

的底部
anime();

^这里是调用setInterval

的整个函数的调用

以上代码是动漫网站所有者使用其RSS源所拥有的聊天室动画片公告的一部分。

上面的代码有效,请原谅我这样说。 我要说“我不知道为什么”。因为我真的不知道为什么setInterval选择并选择何时工作。 我和一个在javascript和基于时间的函数方面比我有更多知识的朋友谈过,他说setInterval没有“条件”可以运行。

for (var epid in eps) {
    epid = parseInt(epid, 10);
    var eptime = (new Date(eps[epid].pubDate[0])*1000)/1000;
    if(eptime > site.lastbuilddate){
        counter = counter+1;
        if(counter < 6){
            var url = eps[epid].link.split('//')[1];
            var keyword = '';
            var words = url.substr(0, url.length-1).split('/').join('-').split('-');
            for (var wid in words) {
                keyword += words[wid].charAt(0);
            }
            http.get({hostname:'dev.ilp.moe', port:80, path:'/surl/yourls-api.php?username=usernameremovedforsecurity&password=passwordremovedforsecurity&format=json&action=shorturl&url='+url+'&title='+ctitle+' - '+eps[epid].title[0]+'&keyword='+keyword}, function(r) {
                if(r.statusCode === 200) { //200 is success
                    var b = '';
                    r.on('data', function(c) {
                        b += c;
                    });
                    r.on('end', function() {
                        list.push(font(colors['normal'])+eps[epid].title[0] +' - ['+ utils.secondsToString((new Date() - (eptime+site.delay))/1000, 1)+' ago.] - http://dev.ilp.moe/surl/'+keyword+'</f>');
                    }
                }
            });
        }
    }
};

以上代码是创建shorturls的部分。

这是正在加载的json数据库。

{"0":{"lastbuilddate":1426441081000,"delay":0,"host":"www.animerush.tv","path":"/rss.xml","chats":["animerushtv"],"interval":15},"1":{"lastbuilddate":1424068119000,"delay":28800000,"host":"dubbedanime.tv","path":"/feed/","chats":["dubbed-anime-tv"],"interval":15},"2":{"lastbuilddate":1426415086000,"delay":32400000,"host":"bestanimes.tv","path":"/feed/","chats":["bestanimestv"],"interval":15},"3":{"lastbuilddate":1426434866000,"delay":0,"host":"www.theanime.tv","path":"/feed/","chats":["timecapsule"],"interval":15}}

最近对我的代码的编辑应该是使用数据库中站点的RSS提要中提供的链接为每个剧集实现缩短链接。

域名http://ilp.moe是我的域名。

我有控制台随处可见,并尽可能多地进行测试。 此时我不明白为什么编辑会使setInterval执行的代码不再被执行。

1 个答案:

答案 0 :(得分:0)

代码未执行的原因是因为函数已分配给变量,因此它们不会运行直到达到setInterval。

当它们到达setInterval时,错误会阻止setInterval执行(取决于错误的严重程度)。

获取该函数并运行它而不将其放入var或setInterval和控制台日志记录中我发现错误是由此行引起的

var url = eps[epid].link.split('//')[1];

在这种情况下

eps[epid].link; // === ["http://blah.com/animelolep1"]

我的问题是var url试图拆分列表而不是字符串

这是修复

var url = eps[epid].link[0].split('//')[1]; // grabs the first item from the list and then splits