【问题标题】:JSON method call is not loadingJSON 方法调用未加载
【发布时间】:2015-09-15 10:36:29
【问题描述】:

这在我的 JavaScript 脚本中,

当我调用函数calculate() 时,JSON 不调用方法ConvertDataTabletoString(),只在我加载页面时执行一次。

我希望每 5 秒调用一次方法 spade。

我在 .aspx 中调用 vb.net 函数。

我该怎么办?

    var map;
    var markers;
    function onClickRow(detailUrl) {
        debugger;
    }

    function calulate() {

        //Call the approve method on the code behind

        markers =  JSON.parse('<%=ConvertDataTabletoString() %>');

        var markerlivo = new google.maps.Marker
        (
            {
                position: new google.maps.LatLng(43.5518760, 10.3080108),
                map: map,
                title: 'Porto Livorno'
            }
        );
        var infowindow = new google.maps.InfoWindow({
            content: 'Location info:Porto Livorno<br/>Country Name:Italy'
        });

        var infowindowLivorno = new google.maps.InfoWindow({
            content: 'Location info:Porto Livorno<br/>Country Name:Italy'
        });

        google.maps.event.addListener(markerlivo, 'click', function () {
            // Calling the open method of the infoWindow 
            infowindowLivorno.open(map, markerlivo);
        });


        for (i = 0; i < markers.length - 1; i++) {
            var data = markers[i]
            var myLatlng = new google.maps.LatLng(markers[i].F_LATPRIMI, markers[i].F_LONPRIMI);
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                title: data.nome
            });
            (function (i, marker) {

                google.maps.event.addListener(marker, 'click', function () {

                    if (!infowindow) {
                        infowindow = new google.maps.InfoWindow();
                    }

                    infowindow.setContent('Latitudine: ' + markers[i].F_LATPRIMI + '<br/>longitudine: ' + markers[i].F_LONPRIMI + '<br/>Data' + markers[i].D_TS + '<br/>Velocità ' + markers[i].F_VELOCITA);

                    infowindow.open(map, marker);

                });

            })(i, marker);

        }
        /*

        Dim Nome As String
        Dim I_ID_NAVE As Integer
        Dim F_LATPRIMI As Double
        Dim F_LONPRIMI As Double
        Dim D_TS As Date
        Dim F_ROTAZIONE As Double
        Dim F_VELOCITA As Double

        */


    }
    function initialize() {

        var mapProp = {
            center: new google.maps.LatLng(43.5518760, 10.3080108),
            zoom: 13,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: true,
            mapTypeControlOptions:
    {
        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
        poistion: google.maps.ControlPosition.TOP_RIGHT,
        mapTypeIds: [google.maps.MapTypeId.ROADMAP,
          google.maps.MapTypeId.TERRAIN,
          google.maps.MapTypeId.HYBRID,
          google.maps.MapTypeId.SATELLITE]
    },
            navigationControl: true,
            navigationControlOptions:
    {
        style: google.maps.NavigationControlStyle.ZOOM_PAN
    },
            scaleControl: true,
            disableDoubleClickZoom: true,
            draggable: true,
            streetViewControl: true,
            draggableCursor: 'move'
        };


        map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
        aggiorna();
    }

    var bool=true
    function aggiorna() {
  //      while (bool === true)
        try{
              setTimeout(aggiorna, 5000)

                calulate();

            }
            catch (e) {
                /*
                * This will happen if the sleep is woken up - you might want to check
                * if enough time has passed and sleep again if not - depending on how
                * important the sleep time is to you.
                */
            }

    }

    google.maps.event.addDomListener(window, 'load', initialize);

【问题讨论】:

    标签: javascript asp.net json google-maps google-maps-api-3


    【解决方案1】:

    您希望每 5 秒调用一次哪个方法? 计算ConvertDataTabletoString

    您可以考虑使用setInterval,例如

    var interval = setInterval(calculate, 5000);
    

    将每 5 秒调用一次 calculate 方法,直到您使用

    清除间隔
    clearInterval(interval);
    

    兄弟。

    【讨论】:

    • 我希望 JSON.parse('');每次启动函数时都会重新加载它,但不会发生 JSON.parse('');它仅在页面加载时启动
    • 看到您的代码,标记不会在每次调用 ConvertDataTabletoString 时神奇地更新,因此您应该考虑在计算方法周围使用 setInterval,并对其进行修改,例如先清空地图,然后每次重新添加新标记5s(如果这是应用程序的重点)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    相关资源
    最近更新 更多