0

According to the Carto.js documentation, layer.invalidate():

"Refreshes the data. If the data has been changed in the CARTO server those changes will be displayed. Nothing happens otherwise. Every time a parameter is changed in a sublayer, the layer is refreshed automatically, so there’s no need to call this method manually."

However, I'm wondering if I'm not using invalidate properly. I have changed data in the CARTO server (ie. table values), however calling layer.invalidate() doesn't refresh my map.

cartodb.createLayer(map, {
    user_name: 'user_name',
    type: 'cartodb',
    sublayers: [
        //Baselayer
        {
        type: "http",
        urlTemplate: 'http://{s}.basemaps.cartocdn.com/light_nolabels/{z}/{x}/{y}.png',
        subdomains: [ "a", "b", "c" ]
        },

        //Projects
        {
        sql: "SELECT * FROM ab",
        cartocss: '#ab {marker-fill: #15B14D;}',
        interactivity: ['name', 'description', 'source', 'contact', 'category'],
        },

        //Labels
        {
        type: "http",
        urlTemplate: 'http://{s}.basemaps.cartocdn.com/light_only_labels/{z}/{x}/{y}.png',
        subdomains: [ "a", "b", "c" ]
        },
        ],
    })
.addTo(map)
.done(function (layer){
    cartodb.vis.Vis.addInfowindow(map, layer, ['name', 'description', 'source', 'contact', 'category'],{
        infowindowTemplate: $('#infowindow_template').html(),
        templateType: 'mustache'});
    layer.invalidate();
    });
1

You need to run the invalidate method within a setTimeout or setInterval functions, as shown in this working example:

setInterval(function(){ 
layer.invalidate(); 
console.log("Update!")
}, 5000);

Because if you place it outside, it will invalidate at the same time you are rendering the map and the result would be the same.

  • To add further context. If you are updating the SQL data from your front end, then you should be calling layer.invalidate() after every update. I would only use the setInterval function if you are regularly updating the data from outside of the front end application. – Jags Sep 20 '17 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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