简体   繁体   中英

pass variable value from inside the loop to outside

I have a for loop and is changing the value of a variable previously declared outside the loop. What I have to do is to show that new value in an overlaid div that is being toggled on and off for a few seconds. Is any way to pass that value from inside the loop to the overlay outside the loop?

<script>
function toggle(id) {
   var e = document.getElementById(id);
   e.style.display = (e.style.display == "block") ? "none" : "block";
}
</script>

<% 
int var=3; 

out.println("<div id='overlay'>");
out.println(var);
out.println("</div>");

for(int i=0;i<3;i++) {
   var++;
   out.println("<script>");
   out.println("toggle('overlay');");
   out.println("</script>");
   Thread.sleep(3000); out.flush();
   out.println("<script>");
   out.println("toggle('overlay');");
   out.println("</script>");
   Thread.sleep(3000); out.flush();
}
%>

You need to dynamically change the content of the div tag via javascript. For this you could probably output

out.println("<script>document.getElementById(\"overlay\").innerHTML = \"" + var + "\"</script> 

That being said .. the whole setup looks a bit weird. What exactly are you trying to achieve?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM