Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-15-2012, 12:22 PM   PM User | #16
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Waiting until all responses have been received:
Code:
<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
<style type="text/css">
    #theDiv {
        width: 500px; height: 50px;
        outline: 1px solid gray;
    }
</style>
<script type="text/javascript">
    
function fncTrim(str) {
	return str.replace(/^\s+|\s+$/g,'');
}

for (var i=0; i < 10; i++) {
    var counter = 1, vals = [];
    (function (req) {
        var thisone = i, complete = false;
        req.onreadystatechange = function () {
            if (req.readyState == 4 && req.status == 200) {
                var results = " " + (thisone+1) + ": " + fncTrim(req.responseText);
                vals.push(results);
                complete = (++counter == 10);
                if (complete) {
                    updating = setInterval(function () {
                        var theDiv = theDiv || document.getElementById('theDiv');
                        // or use table cells and their ids
                        if (vals.length > 0) {
                            theDiv.innerHTML = theDiv.innerHTML + vals.shift();
                        } else {
                            clearInterval(updating);
                        }
                    }, 500);
                }
            }
        };
        req.open("GET", 'createRandomNumber.php?rnd=' + Math.random(), true);
        req.send(null);
    }) (new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP"));
}

</script>
</head>
<body>
<h1>10 Asynchronous Ajax Requests</h1>
<p>Here come the 10 random numbers..</p>
<div id="theDiv">Wait..<br></div>

</body>
</html>
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
CMaso (12-15-2012)
Old 12-15-2012, 04:40 PM   PM User | #17
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
We could even trigger the (set)intervals before all the data has been retrieved:

Code:
complete = (++counter == 5);
..but we would need to ensure data is available (has been returned) before attempting to insert it in the DOM.

However, my code as written will just stop inserting if it runs out of retrieved values.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-15-2012, 06:23 PM   PM User | #18
CMaso
New Coder

 
Join Date: Dec 2009
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
CMaso is an unknown quantity at this point
Very nice! One interesting thing - I don't know if PHP does this, but Coldfusion generates random numbers based on a system timer, so any simultaneous requests will get the same random number. From the screen output, you can see how many requests are hitting the page at the same time:

Wait..
1: The random number is 0.361913578305
2: The random number is 0.361913578305
3: The random number is 0.363346901562
6: The random number is 0.363346901562
5: The random number is 0.363346901562
4: The random number is 0.363346901562
7: The random number is 0.640690712302
8: The random number is 0.640690712302
9: The random number is 0.636390802138
10: The random number is 0.636390802138

Of course, you could do the same thing just by returning the value of that system timer, which in CF is gotten by the getTickCount() function.
CMaso is offline   Reply With Quote
Old 12-15-2012, 06:40 PM   PM User | #19
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
If you use RandRange you can specify which algorithm to use. Or use rand() with randomize() to seed the random number generator.

Quote:
RandRange(number1, number2[, algorithm])

History
ColdFusion MX 7: Added the algorithm parameter.

See also
Rand, Randomize

Parameters
number1, number2
Integer numbers. If the numbers are not in the range -2,147,483,648 - 2,147,483,647, ColdFusion generates an error.

algorithm
(Optional) The algorithm to use to generated the random number. ColdFusion installs a cryptography library with the following algorithms:

CFMX_COMPAT: the algorithm used in ColdFusion (default).
SHA1PRNG: generates a number using the Sun Java SHA1PRNG algorithm. This algorithm provides greater randomness than the default algorithm
IBMSecureRandom: for IBM WebSphere (IBM JVM does not support the SHA1PRNG algorithm.)
Presumably the querystring-parameter you are passing to the other page, which is not currently used, can be used as the seed.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 12-15-2012 at 06:43 PM..
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:29 PM.


Advertisement
Log in to turn off these ads.