1

My java-script function with ajax which has to be moved in to a java class:-

<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>

I need the above code to be executed periodically after server start, so i had used context listner and implemented runnable in a new java class, below is my java class:-

package com.my.classes;

public class obrecord implements Runnable {

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

    }

}

Now, i need to execute the above javascript code in the below java class to run it periodically on server start. is there any method to do it? or is there any alternative efficient method to get the job done? Any piece of code is highly appreciated and thanks in advance.

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
jasim
  • 459
  • 1
  • 6
  • 24

1 Answers1

0

Create a function which is going to be called periodically. and use this function of javascript.

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

There is no need to write java code.

I think this question can help : Is there any way to call a function periodically in JavaScript?

Community
  • 1
  • 1
Pratik Shah
  • 1,782
  • 1
  • 15
  • 33
  • am sorry it wont help. i want the function to run periodically without opening a webpage. i want the function to run periodically all the time which server is running. :( – jasim Oct 14 '14 at 15:27
  • You don't want any webpage, then how are you going to load this javascript ? – Pratik Shah Oct 15 '14 at 03:33
  • there is a method by implementing context listener in java class and extending a new class implementing runnables. it will execute the java code periodically on server run even without opening the webpage but my problem is i cannot write the above javascript code in the java class extended with runnables so i just wanted that java class implemented with runnable to call the function in the javascript so it will run perodically even without opening a webpage – jasim Oct 15 '14 at 14:24