我如何每隔3分钟运行一次代码?

时间:2015-04-22 21:39:11

标签: javascript jquery

var baseData = 'user_id=&user_name=&common_data={"isJailbroken":0,"android_id":"-1","device_type_name":"QMV7B 4.2.2","s_mac":"-1","user_id":20510481089,"sdk_ver":"0104","terrace_type":"google","device_detail_type":"QMV7B","spx_did":826813,"advertising_id":"923000a9-7879-450c-895b-ebd70f32d2b4","mac":"-1","language":"en","device_uid":"923000a9-7879-450c-895b-ebd70f32d2b4","idfa":"-1","device_date":"Tue Apr 21 22:28:36 2015","app_type_name":"app","adId":"923000a9-7879-450c-895b-ebd70f32d2b4","server_id":89,"s_adid":"-1","isPirated":0,"vendorId":"-1","user_name":"Captain Katsura","SAID":"923000a9-7879-450c-895b-ebd70f32d2b4","sh1dId":"-1","md5dId":"-1","pkg_version":"1.7.7","apns_token":"-1","nsuuId":"-1","device_os_version":"4.2.2"}&ex_data=';

                   $('button').on('click', function (clickEvent) {

                    var sid = $('#sid').val();
                    var extraData = '{"planet_id":' + "6_300_6" + '}';          
                    var sign = $.md5(sid + extraData);
                    var finalData = baseData + extraData + "&type=1";
                    var url = "http://ing0042.playercable.com/ING004/n/WebServer/Web/sogame/newControl/nmFleet/getRadarFleets?sign=" + sign;//Needs to be filled

                    $.ajax({
                        method: "POST",
                        url: url,
                        contentType: "application/x-www-form-urlencoded",
                        data: finalData,
                        success: function (response){
                            var json = $.parseJSON(response);
                            var count = json.count;                                                     

                            if (count < 0){
                                //More code
                            }

                        },
                        error: function (){
                            alert ('There was an error!');
                        }
                    });//end ajax

                });//end button click event

简单明了。我熟悉PHP,但对Js和jQuery不熟悉。我需要一些帮助才能让它每隔3分钟运行一次。

我尝试将它包装在setInterval函数中,但它没有用。什么是正确的方法呢?

1 个答案:

答案 0 :(得分:0)

首先,将点击调用中的匿名函数转换为命名函数:

function buttonClick(clickEvent) {

                var sid = $('#sid').val();
                var extraData = '{"planet_id":' + "6_300_6" + '}';          
                var sign = $.md5(sid + extraData);
                var finalData = baseData + extraData + "&type=1";
                var url = "http://ing0042.playercable.com/ING004/n/WebServer/Web/sogame/newControl/nmFleet/getRadarFleets?sign=" + sign;//Needs to be filled

                $.ajax({
                    method: "POST",
                    url: url,
                    contentType: "application/x-www-form-urlencoded",
                    data: finalData,
                    success: function (response){
                        var json = $.parseJSON(response);
                        var count = json.count;                                                     

                        if (count < 0){
                            //More code
                        }

                    },
                    error: function (){
                        alert ('There was an error!');
                    }
                });//end ajax

            }

然后使用setInterval

setInterval( buttonClick, 180000 );
//3 minutes -> 3*60*1000 = 180000

另外,在

上更新你的jQuery
$('button').on('click', buttonClick);