Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How to simulate?

by scorpio17 (Monsignor)
on Apr 20, 2011 at 14:11 UTC ( #900349=note: print w/ replies, xml ) Need Help??


in reply to How to simulate?

This is a job for AJAX. Instead of using document.location to redirect to another page, what you need to do is:

  1. Create an XMLHttpRequest object.
  2. Use that object to make a request (i.e., invoke your perl cgi script).
  3. Define a response handler that will receive the output of your script and insert it into your page, you can create an empty div element to act as a placeholder for this.
  4. After handling each response, setup the next request (this is how you would loop through all your dates)

Here's a simple demo I had laying around that does something similar:

<html> <head> <script type="text/javascript"> function createRequestObject() { var req; if (window.XMLHttpRequest) { // Firefox, Safari, Opera... req = new XMLHttpRequest(); } else if (window.ActiveXObject) { // Internet Explorer 5+ req = new ActiveXObject("Microsoft.XMLHTTP"); } else { // error creating the request object, // (maybe an old browser is being used?) alert('There was a problem creating the XMLHttpRequest object'); req = ''; } return req; } // Make the XMLHttpRequest object var http = createRequestObject(); sendRequest(); function sendRequest() { // Open perl script for requests var now = new Date(); http.open('GET', 'http://' + document.domain + '/cgi-bin/ajax/demo. +pl?nocache='+now.getTime(), true); http.onreadystatechange = handleResponse; http.send(null); } function handleResponse() { if(http.readyState == 4 && http.status == 200){ var response = http.responseText; // Text returned FROM perl scrip +t if(response) { // UPDATE ajaxText content document.getElementById("ajaxText").innerHTML = response; setTimeout('sendRequest()', 5000); } } } </script> </head> <body> <p>The message is...</p> <div id="ajaxText" >Hello World!</div> </body> </html>

This is just a demo. The demo.pl cgi script simply prints out a random text string:

#!/usr/bin/perl use strict; print "content-type: text/html\n\n"; my @messages = ( "This is message 1", "This is message 2", "This is message 3", "This is message 4", "This is message 5", ); # print a random message print @messages[rand @messages] , "\n";

The response handler calls this script every 5 seconds using setTimeout(). Note that I append the epoch time to the cgi script query string so that the browser won't simply cache the first response and use it over and over (that's a common ajax gotcha). When you view the html page in your browser, you should see the message change randomly every 5 seconds.

I hope that helps get you moving in the right direction. Good luck!


Comment on Re: How to simulate?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://900349]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2014-10-24 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls