Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: How to simulate?

by scorpio17 (Abbot)
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();'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 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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://900349]
[hippo]: Are you any good at perl?

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2018-06-23 09:12 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.