Share web page detection and shake instance code _ javascript skills

Source: Internet
Author: User
This article mainly introduces the relevant information about the code of the web page detection and shake instance. If you need more information, you can refer to this article and paste the Code directly. The specific code is as follows:

Var Shaker = function (f) {// SHAKE: it is detected that three shakes are calculated as one shake. After shaking, the handler is called, do not detect this. // f. callback = f; this. status = 0; // 0: listening has not started. 1: listening has started. this. speed = 15; this. lastX = this. lastY = this. larats = 0; this. num = 0; // The number of times this is triggered. minNum = 3; // minimum number of detection triggers this. beginSecond = 0; // The number of seconds to start detection this. maxSecond = 3; // The maximum interval of seconds this. handlerWrap = function () {};} Shaker. prototype. listen = function () {// listen to shake var that = this; if (this. status = 0 & window. deviceMotionEvent) {this. status = 1; this. handlerWrap = function (event) {that. handler (event)} window. addEventListener ('deviceid', this. handlerWrap, false) ;}} Shaker. prototype. release = function () {// stop listening if (this. status = 1) {if (window. deviceMotionEvent) {window. removeEventListener ('deviceid', this. handlerWrap);} this. status = 0; this. num = 0 ;}} Shaker. prototype. reset = function () {// reset Detection if (this. status = 1) {this. num = 0 ;}} Shaker. prototype. handler = function (event) {// sensor event processing if (this. status = 1) {var acceleration = event. accelerationIncludingGravity; var x = acceleration. x; var y = acceleration. y; var z = acceleration. z; if (Math. abs (x-this.lastX)> this. speed | Math. abs (y-this.lastY)> this. speed | Math. abs (z-this.lastZ)> this. speed) {if (this. num = 0) {this. beginSecond = Date. parse (new Date ()/1000} this. num + = 1;} this. lastX = x; this. lastY = y; this. las-1 = z; if (this. num> = this. minNum) {var now = Date. parse (new Date ()/1000; if (now-this. beginSecond <= this. maxSecond) {this. release (); if (this. callback) {this. callback () ;}} this. reset ();}}}

Usage:

Var s = new Shaker (function () {/* callback after shaking */}); s. listen ();

Well, this article will introduce you here, and will be updated continuously in the future. I hope this article will share with you the knowledge about the web page detection and shake instance code, which will be helpful to you.

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.