JavaScript将坐标从一个函数传递到另一个函数

JavaScript passing coordinates from one function to another

本文关键字:函数 一个 另一个 坐标 JavaScript      更新时间:2023-09-26

我有问题传递两个坐标从一个函数到另一个。我真的不知道JavaScript,但它似乎是正确的。你能告诉我我错在哪里吗?

     <head>
      <script>
       var zoom = 12; // 18 for mobile phones because the geolocation is more accurate 
       function init() {
         // Don't bother if the web browser doesn't support cross-document messaging
         if (window.postMessage) {
           if (navigator && navigator.geolocation) {
             try {
               navigator.geolocation.getCurrentPosition(function(pPos) {
               send(pPos.coords.latitude, pPos.coords.longitude);
             }, function() {});
             } catch (e) {}
           } else if (google && google.gears) {
             // Relevant if targeting mobile phones (some of which may have Google Gears)
             try {
               var geoloc = google.gears.factory.create("beta.geolocation");
               geoloc.getCurrentPosition(function(pPos) {
               send(pPos.latitude, pPos.longitude);
             }, function() {});
             } catch (e) {}
           }
         }
       }
       function send(pLat, pLng) {
         var myiframe = document.getElementById("myiframe").contentWindow;
         // The third parameter, zoom, is optional
         myiframe.postMessage(pLat + "," + pLng + "," + zoom, "http://www.qib.la");
       }
       window.onload=init;
      </script>
     </head>
     <body>
      <iframe id="myiframe" src="http://www.qib.la/embed/" width="400" height="400">
        Check the prayer direction towards the Ka'ba in Makkah at
        <a href="http://www.qib.la/">Qibla Direction</a>.
      </iframe>
<script type="text/javascript" src="http://praytimes.org/code/v2/js/PrayTimes.js"></script>
<br>
<p align="center">Waterloo, ON, Canada<p>
<div align="center" id="table"></div>
<script type="text/javascript">
    var date = new Date(); // today
    var times = prayTimes.getTimes(date, pLat + "," + pLng, -5);
    var list = ['Fajr', 'Sunrise', 'Dhuhr', 'Asr', 'Maghrib', 'Isha', 'Midnight'];
    var html = '<table id="timetable">';
    html += '<tr><th colspan="2">'+ date.toLocaleDateString()+ '</th></tr>';
    for(var i in list)  {
        html += '<tr><td>'+ list[i]+ '</td>';
        html += '<td>'+ times[list[i].toLowerCase()]+ '</td></tr>';
    }
    html += '</table>';
    document.getElementById('table').innerHTML = html;
</script>

     </body>

是否可以从异步函数返回一个值?我如何在这里使用回调?

这段代码有一个语法错误:

var times = prayTimes.getTimes(date, + b + ',' + c + , -5);

你有一个"+",然后一个逗号紧跟着它。我不确定你的意图是什么代码,但这是什么导致它不能运行。

也许您打算遵循附加逗号作为字符串的模式?

var times = prayTimes.getTimes(date + ',' + b + ',' + c + ',' + -5);

或者-5是一个单独的参数?

var times = prayTimes.getTimes(date, b + ',' + c, -5);