0

I'm kinda new to the JSP world. I have this web application that calls the page below, the result.jsp page. This page is called after a submit on a form, and the action class generates an image from the createImage method, called by a dispatchParam.

The actual code part is this:

<div id="container_imagem_resultado">
    <p>
        <img src="<%=pathBase%>access.do?dispatchParam=createImage&time="new Date().getTime();>
</div>

I need to create some trigger so the page waits for 1s before calling the img tag.

How can I do it? is there a way to dinamically insert into the HTML response page the tag after that 1s wait? Would adding <%Thread.sleep(1000);%> just before the img tag work?

igorfeiden
  • 177
  • 7
  • 1
    Of course not, the `Thread.sleep` runs on the *server* side. Use a JS timeout and set the source attribute when it fires. – Dave Newton Jun 03 '15 at 17:05
  • Thanks, I understand the idea. But houw could I make it happen in the page? I've seen some ```timeout``` examples on the internet, but couldn't figur out what to do in my case. – igorfeiden Jun 03 '15 at 17:13
  • By writing javascript-I'm not sure what else to tell you, are you familiar with JavaScript? – Dave Newton Jun 03 '15 at 17:19
  • Not quite. I've seen some functions but I don't really understand it. I've seen lots of examples, all of them calling a timeout triggered by a button or something, but I need mine to trigger the moment the page loads. – igorfeiden Jun 03 '15 at 17:20
  • Sound like it is about time to learn :-) – Dave Newton Jun 03 '15 at 17:21
  • Yeah. I accidentally pressed "enter" while writing my answer above, I've edited it. – igorfeiden Jun 03 '15 at 17:22

1 Answers1

0

Doing a delay in Java (server-side) on the page, even in a scriptlet, will only result in a delay in sending the page to the client's browser, which results in either a long pause before they receive the page, or (in some cases) a half-loaded page with an odd delay until the rest is loaded.

To delay the loading of something on the client-side, the best answer is to use Javascript. Given that you've tagged the question , I'm going to assume that jQuery is an available option for you.

For that purpose, this answer gives a reasonable example of what to do here:

// jQuery construct; calls this function after the page is ready.
$(document).ready(function () {
    // Create the function which loads the image.
    function showImage() {
        // Gets a handle for the container div, and replaces its HTML contents.
        // Not the most elegant, but it works.
        $("#container_imagem_resultado").html('<p><img src="<%=pathBase%>access.do?dispatchParam=createImage&time=' + new Date().getTime()  + '" /></p>');
    }

    // Schedule it after 1 second (1000ms).
    setTimeout(showImage, 1000);
});

And the HTML:

<div id="container_imagem_resultado">
    <p>
        <!-- TODO placeholder -->
    </p>
</div>

As a side note, isn't 1 second a little long for a loading delay? Unless you intend for there to be a noticeable lag in the image loading, or there's another reason for loading the image after 1 second, you should make it a lot lower, otherwise it might come across as a broken window.

Community
  • 1
  • 1
Shotgun Ninja
  • 2,540
  • 3
  • 26
  • 32