Skip to content
Advertisement

How can I control this code with “setInterval”?

I tried create a function for use that code in setInterval but when I create a function with that code it stop working. I tried almost everything. Could you guys help me about that?

(async () => {

  let feed = await parser.parseURL(url);


    
  feed.items.forEach(item => {
       const habert = item.pubDate;
       const d2 = new Date(habert)
     
       var diffMs = (d1 - d2);
       var diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
       var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
       
       if(diffMins <= 5 && diffHrs <= 0){
            //console.log(`🔴 Yeni haber geldi: `+'n'+"Başlık: "+ item.title+`n`+"İçerik: "+ item.content+ "İçerik sisteme kayıt edildi."+ `n`+ item.pubDate)
            const haber_baslik = item.title;
            console.log(haber_baslik);
            if(db.get('sonhaber') != haber_baslik){
              db.set('sonhaber', haber_baslik);
            
              bot.sendMessage('@borugazetesi', `🗣 : ${item.title} n n 📰 ${item.content}`);
              
            }else{
              return console.log("Son haber zaten sorunsuz bir şekilde gönderildi.");
          }
           //console.log(sorgu);
          }
           
          
  
  });

//setInterval(haberkontrol, 600);

})();

Advertisement

Answer

You’ve not defined your function properly and you are calling setInterval in the function that you call in set interval.

Can you try this? I hope it will work but I think it is not efficient to check all records for each second.

const habercontrol =  () => {

   parser.parseURL(url).then((feed)=>{

      
        feed.items.forEach(item => {
            const habert = item.pubDate;
            const d2 = new Date(habert)
          
            var diffMs = (d1 - d2);
            var diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
            var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
            
            if(diffMins <= 5 && diffHrs <= 0){
                 //console.log(`🔴 Yeni haber geldi: `+'n'+"Başlık: "+ item.title+`n`+"İçerik: "+ item.content+ "İçerik sisteme kayıt edildi."+ `n`+ item.pubDate)
                 const haber_baslik = item.title;
                 console.log(haber_baslik);
                 if(db.get('sonhaber') != haber_baslik){
                   db.set('sonhaber', haber_baslik);
                 
                   bot.sendMessage('@borugazetesi', `🗣 : ${item.title} n n 📰 ${item.content}`);
                   
                 }else{
                   return console.log("Son haber zaten sorunsuz bir şekilde gönderildi.");
               }
                //console.log(sorgu);
               }
       });


    });
  
  
  };
  
  setInterval(habercontrol, 600);
Advertisement