<p>This is actually a pretty simple task.</p>

<ol>
<li>Create a template that your JS will reach out to returning the total number of entries. First create a template folder called "ajax" with an index.html file inside. Within that file put (replacing blogs with the name of your channel):</li>
</ol>

<p><code>{{ craft.entries.section("blogs") | length }}</code></p>

<ol start="2">
<li><p>Inside your javascript, add this:</p>

<pre><code>    var numberOfEntries = false;

    setInterval(function(){

    $.ajax({
      url: '/ajax',
      type: 'GET',
    })
    .success(function(data) {

      updatedNumberOfEntries = Number(data);

      if(updatedNumberOfEntries != numberOfEntries){

        // something was updated -- do whatever you want
        numberOfEntries = updatedNumberOfEntries;

      }
    });

    }, 5000);
</code></pre></li>
</ol>

<p>You'll obviously need to make some revision to suit whatever you're trying to do but essentially just dump out the number of articles in a template, and use jquery's ajax method to get that value and see if it has changed.</p>

CC BY-SA 3.0
2015-10-05T15:05:28.753Z
2015-10-05T15:05:28.753Z
Bryant
<p>I would like to check periodically (every 5 seconds for example) whether there are new entries in a channel. If yes, I would like to present a refresh button on the website (with the number of the new entries).</p>

<p>So, I probably need a javascript which executes every 5 seconds a database query for the id of the latest entry and compares that to the last entry shown on the current page.</p>

<p>But I guess that javascript is not able to execute a database query, so I probably need a plugin for this.</p>

<p>Are my assumptions more or less correct? Where could I find a paid developper for such a plugin?</p>

<p>Thank you very much for your support.</p>

<ajax>
How to check for new entries periodically from the front-end?
# How to check for new entries periodically from the front-end?

(Asked by: username_0 on 2015-10-05)

I would like to check periodically (every 5 seconds for example) whether there are new entries in a channel. If yes, I would like to present a refresh button on the website (with the number of the new entries).

So, I probably need a javascript which executes every 5 seconds a database query for the id of the latest entry and compares that to the last entry shown on the current page.

But I guess that javascript is not able to execute a database query, so I probably need a plugin for this.

Are my assumptions more or less correct? Where could I find a paid developper for such a plugin?

Thank you very much for your support.

**Tags:** ajax


---

## 1 answers

### Answer by username_1 on 2015-10-05. Score: 4 | Accepted answer

This is actually a pretty simple task.

Create a template that your JS will reach out to returning the total number of entries. First create a template folder called "ajax" with an index.html file inside. Within that file put (replacing blogs with the name of your channel):

{{ craft.entries.section("blogs") | length }}

Inside your javascript, add this:
    var numberOfEntries = false;

    setInterval(function(){

    $.ajax({
      url: '/ajax',
      type: 'GET',
    })
    .success(function(data) {

      updatedNumberOfEntries = Number(data);

      if(updatedNumberOfEntries != numberOfEntries){

        // something was updated -- do whatever you want
        numberOfEntries = updatedNumberOfEntries;

      }
    });

    }, 5000);


You'll obviously need to make some revision to suit whatever you're trying to do but essentially just dump out the number of articles in a template, and use jquery's ajax method to get that value and see if it has changed.


---

