YUI Library Home

Site Search (YDN & YUIBlog):

# 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).

View example in new window.

## 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:

<script src="yahoo.js"></script> <script src="event.js"></script> <script src="connection.js"></script>

### 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.

var handleSuccess = function(o){ if(o.responseText !== undefined){ div.innerHTML = "Transaction id: " + o.tId; div.innerHTML += "HTTP status: " + o.status; div.innerHTML += "Server response: " + o.responseText; div.innerHTML += "Argument object: property foo = " + o.argument.foo + "and property bar = " + o.argument.bar; } } var handleFailure = function(o){ div.innerHTML += "<li>Transaction id: " + o.tId + "</li>"; div.innerHTML += "<li>HTTP status: " + o.status + "</li>"; div.innerHTML += "<li>Status code message: " + o.statusText + "</li>"; } var callback = { success:handleSuccess, failure: handleFailure, argument: { foo:"foo", bar:"bar" }, timeout: 1500 };

### 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".

var sUrl = "php/sync.php"; var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);

### YUI Logger Output:

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

Reload with logging
and debugging disabled.

### Connection Manager Examples:

- Connection Manager GET Transaction
- Connection Manager POST Transaction
- Connection Manager Transaction Timeout
- Retrieving a Yahoo! Weather RSS Feed
- Subscribing to Connection Manager Global Events
- Subscribing to Connection Manager's Custom Events via the Callback Object
- Connection Manager assisted image crop (included with examples for ImageCropper Control (beta))
- Dynamically Loading Node Data (included with examples for TreeView Control)
- Editor Data Post with Connection Manager (included with examples for Rich Text Editor)
- Simple Application (included with examples for Layout Manager)

### More Connection Manager Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2008 Yahoo! Inc. All rights reserved.

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