I am quite confused with when to call my JS code. I have the following

Zepto(function($) {
  document.addEventListener("deviceready", onDeviceReady, false);
});

function onDeviceReady() {
  initalizeData();
  FastClick.attach(document.body);
  startSnapper();
  $('input[name=topcoat]').change(function(){ schoolValueChanged() });
}

When the device is ready, deviceready is triggered. The function onDeviceReady sets up my Javascript.

Is that the right way of doing it?

有帮助吗?

解决方案

In the head of your page between your script tags you should just have the event listener, it shouldn't be inside anything else. Here is how it should look:

<script>
  document.addEventListener("deviceready", onDeviceReady, false);

  function onDeviceReady() {
    initalizeData();
    FastClick.attach(document.body);
    startSnapper();
    $('input[name=topcoat]').change(function(){schoolValueChanged() });
  }
</script>

That's the way I've seen it in the documentation and that's the way I've done it in all of my Cordova projects. And if you're using any other event listeners they should be added after deviceready is called.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top