문제

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