2

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.

1 Answer 1

4

This is actually a pretty simple task.

  1. 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 }}

  1. 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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.