YUI recommends YUI 3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

YUI Library Home

YUI Library Examples: Connection Manager: Connection Manager Transaction Timeout

Connection Manager: Connection Manager Transaction Timeout

This example illustrates Connection Manager's built-in transaction timeout functionality.

Click the "Create Two Transactions" button below. Two requests will be made to a PHP script that is designed to respond slowly, waiting between 0 and 10 seconds to respond. If the response takes longer than 1.5 seconds, the request will automatically abort (resulting in a "transaction aborted" message).

Setting Transaction Timeouts with Connection Manager

The following code example provides a step-by-step approach to presetting a transaction timeout.

Source file and dependencies

Load the Yahoo Global Object and Connection Manager source files:

1<script src="yahoo.js"></script> 
2<script src="event.js"></script> 
3<script src="connection.js"></script> 
view plain | print | ?

The Callback Object

The callback object includes a timeout property that allows you to specify the amount of time you're willing to wait for a transaction to complete before aborting. To cause a transaction to automatically timeout, the timeout property must be defined wih a value in millseconds. This example defines timeout with a value of 5000(milliseconds). If the transaction is not complete within 5000ms, it will be aborted.

1var handleSuccess = function(o){ 
2    if(o.responseText !== undefined){ 
3        div.innerHTML = "Transaction id: " + o.tId; 
4        div.innerHTML += "HTTP status: " + o.status; 
5        div.innerHTML += "Server response: " + o.responseText; 
6        div.innerHTML += "Argument object: property foo = " + o.argument.foo + 
7                         "and property bar = " + o.argument.bar; 
8    } 
9
10 
11var handleFailure = function(o){ 
12    div.innerHTML += "<li>Transaction id: " + o.tId + "</li>"
13    div.innerHTML += "<li>HTTP status: " + o.status + "</li>"
14    div.innerHTML += "<li>Status code message: " + o.statusText + "</li>"
15
16 
17var callback = 
18
19  success:handleSuccess, 
20  failure: handleFailure, 
21  argument: { foo:"foo", bar:"bar" }, 
22  timeout: 1500 
23}; 
view plain | print | ?

Initiate the Transaction

Call YAHOO.util.Connect.asyncRequest to send the request to sync.php. The PHP script will return a string message after a random delay of 0 to 10 seconds if the transaction was not aborted. If the transaction was successfully aborted, the response object's status property will report -1 and the statusText property will report "transaction aborted".

1var sUrl = "php/sync.php"
2var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback); 
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

YUI Logger Output:

Logger Console

INFO 66ms (+13) 2:37:00 PM:

LogReader instance0

LogReader initialized

INFO 53ms (+2) 2:37:00 PM:

Get

Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 51ms (+0) 2:37:00 PM:

Get

attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 51ms (+48) 2:37:00 PM:

Get

_next: q0, loaded: undefined

INFO 3ms (+3) 2:37:00 PM:

example

As you interact with this example, relevant steps in the process will be logged here.

INFO 0ms (+0) 2:37:00 PM:

global

Logger initialized

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More Connection Manager Resources:

Copyright © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings