翻牌器自动刷新数据前后端执行的解决方案!
- 1.设置ajax传递随机数
actNum
到后台,无需返回值;
/*定时更新虚拟数据*/ function updateTrans(actNum) { $.ajax({ type: "post", async: true, url: "./api/api.php?act=updateTrans&token=3cab7ce4142608c0f40c785b5ab5ca24&actNum=" + actNum, data: {}, dataType: "json", success: function (res) { //无需返回; }, error: function (err) { console.log("Ajax err:" + err); } }); }
- 2.后台接受actNum,更新数据;
/*定时刷新年度和月度虚拟交易量*/ public function updateTrans() { global $db, $res; dbc(); $actNum = $_GET['actNum']; //更新插件交易量; $row = $db->fetch('orders', 'orders_id, orders_price,orders_virtual', array("orders_virtual" => '1'), ' orders_id DESC'); if ($row) { $row['orders_price'] = $row['orders_price'] + $actNum; $db->update('orders', array('orders_price' => $row['orders_price']), array('orders_id' => $row['orders_id'])); $res["data"] = $actNum; } die(json_encode_lockdata($res)); }
- 3.前台调用参数,产生随机数
function getData() { $.ajax({ url: './api/api.php?act=getYears&token=3cab7ce4142608c0f40c785b5ab5ca24', type: 'POST', async: true, dataType: "json", success: function (res) { var totalNum = parseFloat(parseFloat(res.data[0].total) + parseFloat(yearsNumInt)).toFixed(2); var numRun = $(".numberRun").numberAnimate({num: totalNum, dot: 2, speed: 2000, symbol: ","}); numRun.resetData(totalNum); }, error: function (err) { console.log(err + "请求数据失败!"); }, }); }
- 4.定时刷新数据
//定时刷新数据; var timer; getNewData(); clearInterval(timer); timer = setInterval(getNewData, 6 * 1000);//每隔3秒执行一次 //更新插件数据; function getNewData() { getData(); var actNum = parseFloat((Math.random() * 10 + 5).toFixed(2) - 0);//随机累加2位数; updateTrans(actNum); }
Done!