javascript - 检测移动震动加速度计5秒钟,然后发出警报消息

标签 javascript php jquery mobile shake

我正在开发一个应用程序,用户可以在其中摇晃手机一段时间,例如5秒钟,然后晃动事件计算其总晃动时间,然后显示警报消息,我正在使用晃动js库,但是它会改变产品单次摇动后,我想计算设备的总摇动时间,然后显示一些消息。

请任何人帮助我解决此问题,请务必坚持。
以下是我到目前为止编写的代码。

function changeProduct() {
    var shirts = [
                  ["images/1.jpg","12.65"],
                  ["images/2.jpg","10.00"],
                  ["images/3.jpg","15.50"],
                  ["images/4.jpg","12.65"],
                  ["images/5.jpg","13.00"],
                  ["images/6.jpg","11.85"]
                  ];

    var pos = Math.floor((Math.random() * shirts.length) + 0);
    $("#image").html($("<img/>").attr("src", shirts[pos][0]));
    $(".price").html("$" + shirts[pos][1]);
}

$(document).ready(function() {
    function onShake() {
        changeProduct();
    }

    $.shake({
        callback: function() {
            onShake();
        }
    });
});

最佳答案

您应该在函数外部的某个地方进行如下计算:

var totalShakeTime = 0;


当显示结果时,在显示结果之前:

totalShakeTime += currentShakeTime; //where currentShakeTime represents the last value

关于javascript - 检测移动震动加速度计5秒钟,然后发出警报消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36100701/

相关文章:

jquery - fullcalendar,在工具栏后插入复选框

javascript - 使用 JS 更改 CSS 值的最佳方法?

javascript - 用于数据存储的隐藏 html 表

php - 使用 php Http 连接连接到 Asterisk 管理器界面

javascript - 无法使用 jQuery 和 AJAX 发送 POST 请求

php - 碳 : displaying hours and minutes?

javascript - 使用退格键或删除删除日期

jQuery焦点事件进入无限递归

javascript - 如何控制元素不出现错误?

javascript - 如何在过滤器中使用数组中的值来减少重复代码