为什么我的 JavaScript 代码会过早触发?

发布于 2025-01-08 03:20:24 字数 2215 浏览 1 评论 0原文

我在 onDeviceReady 中添加了一个事件监听器来监听后退按钮事件,并在按下时用 stopWatch 进行响应。但是,它会在应用程序启动时做出响应。

    <!DOCTYPE html>
<html>
  <head>
    <title>UNH BSApp</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    // The watch id references the current `watchAcceleration`
    var watchID = null;

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {
        document.addEventListener("backbutton", stopWatch(), false);
        startWatch();
    }

    // Start watching the acceleration
    //
    function startWatch() {
        document.addEventListener("menubutton", stopWatch(), false);
        // Update acceleration every 0.1 seconds
        var options = { frequency: 10 };

        watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    }

    // Stop watching the acceleration
    //
    function stopWatch() {
        alert("Hello!");
        if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;
        }
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration) {
        var element = document.getElementById('accelerometer');
        element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
                            'Acceleration Y: ' + acceleration.y + '<br />' +
                            'Acceleration Z: ' + acceleration.z + '<br />' +
                            'Timestamp: '      + acceleration.timestamp + '<br />';
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
    <style>
        #start {
            display:block;
            border:solid;
        }
    </style>
  </head>
  <body>
    <div id="accelerometer">Waiting for accelerometer...</div>
    <div id="start">Start</div>
  </body>
</html>

I have an event listener added in onDeviceReady to listen for back button event, and respond with stopWatch when it is pressed. However it responds when the app is started.

    <!DOCTYPE html>
<html>
  <head>
    <title>UNH BSApp</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

    // The watch id references the current `watchAcceleration`
    var watchID = null;

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {
        document.addEventListener("backbutton", stopWatch(), false);
        startWatch();
    }

    // Start watching the acceleration
    //
    function startWatch() {
        document.addEventListener("menubutton", stopWatch(), false);
        // Update acceleration every 0.1 seconds
        var options = { frequency: 10 };

        watchID = navigator.accelerometer.watchAcceleration(onSuccess, onError, options);
    }

    // Stop watching the acceleration
    //
    function stopWatch() {
        alert("Hello!");
        if (watchID) {
            navigator.accelerometer.clearWatch(watchID);
            watchID = null;
        }
    }

    // onSuccess: Get a snapshot of the current acceleration
    //
    function onSuccess(acceleration) {
        var element = document.getElementById('accelerometer');
        element.innerHTML = 'Acceleration X: ' + acceleration.x + '<br />' +
                            'Acceleration Y: ' + acceleration.y + '<br />' +
                            'Acceleration Z: ' + acceleration.z + '<br />' +
                            'Timestamp: '      + acceleration.timestamp + '<br />';
    }

    // onError: Failed to get the acceleration
    //
    function onError() {
        alert('onError!');
    }

    </script>
    <style>
        #start {
            display:block;
            border:solid;
        }
    </style>
  </head>
  <body>
    <div id="accelerometer">Waiting for accelerometer...</div>
    <div id="start">Start</div>
  </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

客…行舟 2025-01-15 03:20:24
 document.addEventListener("menubutton", stopWatch(), false);

这会立即调用 stopWatch 函数。你想要这样的东西:

 document.addEventListener("menubutton", stopWatch, false);
                                                 ^^ no parens!
 document.addEventListener("menubutton", stopWatch(), false);

This calls the stopWatch function immediately. You want something like:

 document.addEventListener("menubutton", stopWatch, false);
                                                 ^^ no parens!
心不设防 2025-01-15 03:20:24

从侦听器的设置中删除 () - 这是调用函数而不将其作为参数传递:

document.addEventListener("menubutton", stopWatch, false);

Remove the () from the setup of the listener - this is calling the function not passing it as an argument :

document.addEventListener("menubutton", stopWatch, false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文