如何在Java类中实现runnable运行我的javascript函数?

时间:2014-10-14 10:21:32

标签: java javascript jsp runnable servletcontextlistener

我的带有ajax的java脚本函数必须移入java类: -

<script type="text/javascript">




$(document).ready(function(){


var polyLat = new Array();
polyLat[0] = 10.194027;
polyLat[1] = 10.226975;
polyLat[2] = 10.059987;
polyLat[3] = 10.002248;
polyLat[4] = 9.854925;
polyLat[5] = 9.835443;
polyLat[6] = 9.899107;
polyLat[7] = 9.993088;
polyLat[8] = 10.081425;
polyLat[9] = 9.992266;
polyLat[10] = 10.194027;//First point repeated to close polygon
var polySides = (polyLat.length)-1;//number of points in polygon
//vertical Longitude coordinates of polygon 
var polyLng =  new Array();
polyLng[0] = 76.201205;
polyLng[1] = 76.375022;
polyLng[2] = 76.775730;
polyLng[3] = 76.778940;
polyLng[4] = 76.584336;
polyLng[5] = 76.411473;
polyLng[6] = 76.368070;
polyLng[7] = 76.397007;
polyLng[8] = 76.317492;
polyLng[9] = 76.267905;
polyLng[10] = 76.201205;//First point repeated to close polygon
//Coordinates for bounding box
var maxLat = Math.max.apply(null,polyLat);  
var minLat = Math.min.apply(null,polyLat);
var maxLng = Math.max.apply(null,polyLng);
var minLng = Math.min.apply(null,polyLng);


$.post('outboundupd.jsp',
        {
    mx_lat:maxLat,
    mn_lat:minLat,
    mx_lng:maxLng,
    mn_lng:minLng,
    ply_sds:polySides
        },
        function(response,status,xhr)
        {
//          alert(response.trim());
            plotdata(response);


});

    function plotdata(response)
    {
        var x;
        var y;
        var mob;
        var jsonArray=JSON.parse(response.trim());
        var jalen= jsonArray.length; 
        for(i=0;i<jalen;i++)
        {
            var obj=jsonArray[i];
            pcode= obj.Pcode;
            nplate= obj.N_plate;
            driver= obj.Driver;
            mob= obj.MobileNu;
            x= obj.Latitude;
            y= obj.Longitude;
            time= obj.Time;

        }


        var j = polySides-1 ;
          oddNodes = 0;
          for (i=0; i<polySides; i++) {
            if (polyLng[i]<y && polyLng[j]>=y  ||  polyLng[j]<y && polyLng[i]>=y) {
                if (polyLat[i]+(y-polyLng[i])/(polyLng[j]-polyLng[i])*(polyLat[j]-polyLat[i])<x)  {
                    oddNodes=!oddNodes; 
                }
            }
           j=i; }




            if(oddNodes!=true)
            {
//              alert("ob mobile:"+mob);

                $.post('obsouth.jsp',
                        {

                    pcd:pcode,
                    npt:nplate,
                    drv:driver,
                    mobl:mob,
                    lat:x,
                    lon:y,
                    tm:time

                        },
                        function(response,status,xhr)
                        {
                            alert(response.trim());


                });

            }

          return oddNodes;


        }

});

</script>

我需要在服务器启动后定期执行上面的代码,所以我使用了上下文列表器并在新的java类中实现了runnable,下面是我的java类: -

package com.my.classes;

public class obrecord implements Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub

    }

}

现在,我需要在下面的java类中执行上面的javascript代码,以便在服务器启动时定期运行它。有没有办法做到这一点?或者有没有其他有效的方法来完成工作?任何代码都受到高度赞赏,并提前感谢。

1 个答案:

答案 0 :(得分:0)

创建一个将定期调用的函数。 并使用javascript的这个功能。

var intervalID = setInterval(function_name(), 5000);

无需编写java代码。

我认为这个问题有助于: Is there any way to call a function periodically in JavaScript?