教程·Javascript 中文手册
Provides a way to lock a critical section of code.
| 服务器端对象 | -- |
| 实现版本 | Netscape Server 3.0 |
The Lock constructor:
Lock();
无。
Failure to construct a new Lock object indicates an internal JavaScript error, such as out of memory.
| Obtains the lock. |
| Verifies that this Lock object was properly constructed. |
| Releases the lock. |
Obtains the lock. If someone else has the lock, this method blocks until it can get the lock, the specified timeout period has elapsed, or an error occurs.
| 方法源 | Lock |
| 实现版本 | Netscape Server 3.0 |
lock(timeout)
| timeout | An integer indicating the number of seconds to wait for the lock. If 0, there is no timeout; that is, the 方法 waits indefinitely to obtain the lock. The default value is 0, so if you do not specify a value, the 方法 waits indefinitely. |
True if it succeeds in obtaining the lock within the specified timeout. False if it did not obtain the lock.
You can obtain a lock for an object to ensure that different clients do not access a critical section of code simultaneously. When an application locks an object, other client requests must wait before they can lock the object.
Note that this mechanism requires voluntary compliance by asking for the lock in the first place.
Verifies that this Lock object was properly constructed.
| 方法源 | Lock |
| 实现版本 | Netscape Server 3.0 |
isValid()
无。
True, if this object was properly constructed; otherwise, false.
It is very rare that your Lock object would not be properly constructed. This happens only if the runtime engine runs out of system resources while creating the object.
This code creates a Lock object and verifies that nothing went wrong creating it:
// construct a new Lock and save in project
project.ordersLock = new Lock();
if (! project.ordersLock.isValid()) {
// Unable to create a Lock. Redirect to error page
...
}
Releases the lock.
| 方法源 | Lock |
| 实现版本 | Netscape Server 3.0 |
unlock()
无。
False if it fails; otherwise, true. Failure indicates an internal JavaScript error or that you attempted to unlock a lock that you don't own.
If you unlock a lock that is unlocked, the resulting behavior is undefined.