[SOLVED] How can I control this code with "setInterval"?

Issue

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

})();

Solution

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

Answered By – Velat Necmettin Kanat

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.