A small error in the JavaScript Advanced Programming Touch Event

Source: Internet
Author: User

Recently, I have been reading Nicholas's "JavaScript Advanced Program Design", is really a good book, easy to understand, well-organized than the "JavaScript authoritative guide" good understanding of some, of course, "JavaScript authoritative guide" has the advantages of authoritative guidance, not gossip, into the topic.

Read the technical book I usually like to write the code on the book, although it takes time, but only when the hand knocks, the brain has a variety of chaotic ideas to jump out, why not according to their own thinking of writing, why the code is better and more elegant, there is no other way to solve the problem before, think more, understanding is very deep, Of course, the most important thing is to remember the prison. Even if it is very easy to understand the code, I also knocked again, there is no way to force the disease hurt.

In the 417 pages, 13.4.9 section, there is a piece of code about the touch and gesture events in JavaScript that doesn't work on my side. Later bold speculation should be a mistake in the book, first knock out, if I was wrong, I hope the great God help correct.

The code in the book is as follows:

functionHandletouchevent (event) {//track only One Touchif(Event.touches.length = = 1){
    varOutput = document.getElementById ("Output");
      Switch(Event.type) {
Case"Touchstart": output.innerhtml= "Touch started (" + Event.touches[0].clientx + "," + Event.touches[0].clienty + ")"; Break; Case"Touchend": output.innerhtml+ = "<br>touch ended (" +event.changedtouches[0].clientx + "," +event.changedtouches[0].clienty + ")"; Break; Case"Touchmove": Event.preventdefault ();//Prevent scrollingoutput.innerhtml + = "<br>touch Moved (" +event.changedtouches[0].clientx + "," +event.changedtouches[0].clienty + ")"; Break; }}}eventutil.addhandler (document,"Touchstart", handletouchevent); Eventutil.addhandler (document,"Touchend", handletouchevent); Eventutil.addhandler (document,"Touchmove", handletouchevent);

The place of error is here:

 

When there is no problem when triggering the Touchstart and Touchmove events, the program can correctly enter the IF and execute the corresponding statement according to the case, but when the Touchend event is triggered, Event.touches.length is already equal to 0, can not enter if, can not execute the statement in case, so trigger touchend will never execute the program. The right program should be

if (event.touches.length = = 1| | event.touches.length==0) {...}

I used the Chrome browser when I tested it because the conditions were not really measured by the phone. If I understand the wrong place, I hope you will point out. Once again, salute to Nicholas.

A small error in the JavaScript Advanced Programming Touch Event

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.