How to use setInterval in EJS file?












0















I have a problem with using setInterval in my EJS file, which is part of my nodejs app (with Express). I have created function getRandomSubarray, which chooses randomly subset of images from array. I want to change this subset every three seconds. This is problematic piece of my code:



<%setInterval(function(){%>
<%RandomSubrecipesImg=tools.getRandomSubarray(recipesImg,4)%>

<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[0].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[1].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3"><img class="navbar-image" src="<%=RandomSubrecipesImg[2].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[3].replace('public',"")%>"></div>
<% },3000);%>



In browser I haven't received any divs. In case of removing setInterval I have div elements in browser, so I think there is something wrong with setInterval. Do you have any idea what is wrong
?










share|improve this question

























  • Possible duplicate of node.js: how to use setInterval and clearInterval?

    – Feras Al Sous
    Jan 20 at 12:20






  • 1





    EJS is a template language, it creates static HTML markup. You cannot do any dynamic stuff with it, and you cannot use setInterval. If you want the divs to change randomly every few seconds in the browser, you will need a clientside script that does that.

    – Bergi
    Jan 20 at 14:41
















0















I have a problem with using setInterval in my EJS file, which is part of my nodejs app (with Express). I have created function getRandomSubarray, which chooses randomly subset of images from array. I want to change this subset every three seconds. This is problematic piece of my code:



<%setInterval(function(){%>
<%RandomSubrecipesImg=tools.getRandomSubarray(recipesImg,4)%>

<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[0].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[1].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3"><img class="navbar-image" src="<%=RandomSubrecipesImg[2].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[3].replace('public',"")%>"></div>
<% },3000);%>



In browser I haven't received any divs. In case of removing setInterval I have div elements in browser, so I think there is something wrong with setInterval. Do you have any idea what is wrong
?










share|improve this question

























  • Possible duplicate of node.js: how to use setInterval and clearInterval?

    – Feras Al Sous
    Jan 20 at 12:20






  • 1





    EJS is a template language, it creates static HTML markup. You cannot do any dynamic stuff with it, and you cannot use setInterval. If you want the divs to change randomly every few seconds in the browser, you will need a clientside script that does that.

    – Bergi
    Jan 20 at 14:41














0












0








0








I have a problem with using setInterval in my EJS file, which is part of my nodejs app (with Express). I have created function getRandomSubarray, which chooses randomly subset of images from array. I want to change this subset every three seconds. This is problematic piece of my code:



<%setInterval(function(){%>
<%RandomSubrecipesImg=tools.getRandomSubarray(recipesImg,4)%>

<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[0].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[1].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3"><img class="navbar-image" src="<%=RandomSubrecipesImg[2].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[3].replace('public',"")%>"></div>
<% },3000);%>



In browser I haven't received any divs. In case of removing setInterval I have div elements in browser, so I think there is something wrong with setInterval. Do you have any idea what is wrong
?










share|improve this question
















I have a problem with using setInterval in my EJS file, which is part of my nodejs app (with Express). I have created function getRandomSubarray, which chooses randomly subset of images from array. I want to change this subset every three seconds. This is problematic piece of my code:



<%setInterval(function(){%>
<%RandomSubrecipesImg=tools.getRandomSubarray(recipesImg,4)%>

<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[0].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[1].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3"><img class="navbar-image" src="<%=RandomSubrecipesImg[2].replace('public',"")%>"></div>
<div class="navbar-image-box col-sm-3 d-none d-sm-block"><img class="navbar-image" src="<%=RandomSubrecipesImg[3].replace('public',"")%>"></div>
<% },3000);%>



In browser I haven't received any divs. In case of removing setInterval I have div elements in browser, so I think there is something wrong with setInterval. Do you have any idea what is wrong
?







javascript node.js express ejs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 14:34







rogkar

















asked Jan 20 at 12:19









rogkarrogkar

32




32













  • Possible duplicate of node.js: how to use setInterval and clearInterval?

    – Feras Al Sous
    Jan 20 at 12:20






  • 1





    EJS is a template language, it creates static HTML markup. You cannot do any dynamic stuff with it, and you cannot use setInterval. If you want the divs to change randomly every few seconds in the browser, you will need a clientside script that does that.

    – Bergi
    Jan 20 at 14:41



















  • Possible duplicate of node.js: how to use setInterval and clearInterval?

    – Feras Al Sous
    Jan 20 at 12:20






  • 1





    EJS is a template language, it creates static HTML markup. You cannot do any dynamic stuff with it, and you cannot use setInterval. If you want the divs to change randomly every few seconds in the browser, you will need a clientside script that does that.

    – Bergi
    Jan 20 at 14:41

















Possible duplicate of node.js: how to use setInterval and clearInterval?

– Feras Al Sous
Jan 20 at 12:20





Possible duplicate of node.js: how to use setInterval and clearInterval?

– Feras Al Sous
Jan 20 at 12:20




1




1





EJS is a template language, it creates static HTML markup. You cannot do any dynamic stuff with it, and you cannot use setInterval. If you want the divs to change randomly every few seconds in the browser, you will need a clientside script that does that.

– Bergi
Jan 20 at 14:41





EJS is a template language, it creates static HTML markup. You cannot do any dynamic stuff with it, and you cannot use setInterval. If you want the divs to change randomly every few seconds in the browser, you will need a clientside script that does that.

– Bergi
Jan 20 at 14:41












1 Answer
1






active

oldest

votes


















1














EJS is used for render things server-side. But setInterval is a client-side function which doenst need to be rendered.



Prepare your data serverside and send everything in init request, then start setInterval client-side and fetch the "new" data from your API - Endpoint. Could be done via AJAX or WebSockets.



Or you could setup an WebSocket and push "new" data to all clients.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276357%2fhow-to-use-setinterval-in-ejs-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    EJS is used for render things server-side. But setInterval is a client-side function which doenst need to be rendered.



    Prepare your data serverside and send everything in init request, then start setInterval client-side and fetch the "new" data from your API - Endpoint. Could be done via AJAX or WebSockets.



    Or you could setup an WebSocket and push "new" data to all clients.






    share|improve this answer




























      1














      EJS is used for render things server-side. But setInterval is a client-side function which doenst need to be rendered.



      Prepare your data serverside and send everything in init request, then start setInterval client-side and fetch the "new" data from your API - Endpoint. Could be done via AJAX or WebSockets.



      Or you could setup an WebSocket and push "new" data to all clients.






      share|improve this answer


























        1












        1








        1







        EJS is used for render things server-side. But setInterval is a client-side function which doenst need to be rendered.



        Prepare your data serverside and send everything in init request, then start setInterval client-side and fetch the "new" data from your API - Endpoint. Could be done via AJAX or WebSockets.



        Or you could setup an WebSocket and push "new" data to all clients.






        share|improve this answer













        EJS is used for render things server-side. But setInterval is a client-side function which doenst need to be rendered.



        Prepare your data serverside and send everything in init request, then start setInterval client-side and fetch the "new" data from your API - Endpoint. Could be done via AJAX or WebSockets.



        Or you could setup an WebSocket and push "new" data to all clients.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 20 at 14:39









        Sebastian WaldbauerSebastian Waldbauer

        365312




        365312
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276357%2fhow-to-use-setinterval-in-ejs-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            Petrus Granier-Deferre