As is used to compile the client in web games. Its Io operations mainly refer to network communication.
I encountered some problems in my project a few days ago.
There is no problem when testing the project on the local machine. After being uploaded to the internet server, the problem occasionally occurs.
The error code is similar to the following:
Buttom. addeventlistener (mouseevent. Click, prohandler); // This is not the real code in the project. You can give an example at will, so the variable name will compare 2
Function prohandler (Event: Event): void {
Socket. Send (data); // 1
Buttom. removeeventlistener (mouseevent. Click, prohandler); // 2
// Many other operations
//.......
}
Such code. Logically, there is no error.
However, problems may still occur.
When you click the buttom. The callback function prohandler is run.
The main operation of the prohandler function is socket. Send (data), which performs a network I/O operation.
The problem is that you can click buttom twice quickly. It may be a cup.
Because the first click, the callback function may run to socket. Send (data), and the second click calls the callback method.
This event is expected to have only one click. But now it has changed to two, and sent two data.
The server received the data twice. Errors may occur.
Therefore, we recommend that you write the prohandler method as follows.
Function prohandler (Event: Event): void {
Buttom. removeeventlistener (mouseevent. Click, prohandler); // 2
Socket. Send (data); // 1
// Many other operations
//.......
}
Change the execution order between 1 and 2. This is the case when you click twice.
The prohandler method removes the event from the beginning at 1.
This is the second event. Data is only sent once.
OK, turn these into a habit ..
Thank you for coming to the cold house.
If you have any errors or comments, please kindly advise