我一直在用刷新我的游戏时遇到一些问题。起初,我试着每次回应整个剧本......但那并不美妙。现在我试图简单地用Ajax请求更新变量。我对Ajax代码:使用Ajax/PHP更新Javascript变量
<script>
$(document).ready(function(){
refreshMap();
});
function refreshMap(){
$('#mapVars').load('<?php print"$game_link_play" ?>/ajax/getPrepareMap.php?id=<?php print"$game_id";?>', function(){
setTimeout(refreshMap, 5000);
});
}
</script>
getPrepareMap.php:
<?php
echo"
<script>
var nationColors = {
'DE': '#990000',
'GB': '#009999',
'FR': '#009999',
};
var nationLeaders = {
'DE': 'Adolf Hitler',
'GB': 'Winston Churchill',
'FR': 'Winston Churchill',
};
var nationPopulation = {
'DE': '81,000,000',
'GB': '54,000,000',
'FR': '64,000,000',
}
</script>
";
?>
用于加载地图的脚本:
<script>
$(function(){
function chkLeader(val){
if (typeof val ==="undefined"){
x = "No Leader";
}
else {
x = val;
}
return x;
};
markers = [
{latLng: [52.50, 13.39], name: 'Berlin'},
{latLng: [51.5, 0.13], name: 'London'},
{latLng: [48.85, 2], name: 'Paris'}
],
cityData = [
100,
100,
100
]
map = new jvm.WorldMap({
container: $('#map'),
map: 'europe_mill_en',
backgroundColor: '#1C6BA0',
zoomOnScroll: false,
markers: markers,
regionStyle: {
initial: {
fill: '#78B6A4',
}
},
markerStyle: {
initial: {
fill: 'yellow'
}
},
series: {
regions: [{
attribute: 'fill'
}],
markers: [{
attribute: 'r',
scale: [5, 6],
values: cityData
}]
},
onRegionLabelShow: function(event, label, code){
label.html(
'<strong>' +
label.html() +
'</strong>' + '<br>' +
'<strong>' +
'Leader: ' +
'</strong>' +
chkLeader(nationLeaders[code]) + '<br>' +
'<strong>' +
'Population: ' +
'</strong>' +
nationPopulation[code]
);
}
});
map.series.regions[0].setValues(nationColors);
});
编辑:我可以得到新的变量引入页面,但地图不会刷新?
新代码对于Ajax:
$(document).ready(function(){
refreshMap();
});
function refreshMap(){
$('#mapVars').load('<?php print"$game_link_play" ?>/ajax/getPrepareMap.php?id=<?php print"$game_id";?>', function(){
setTimeout(refreshMap, 5000);
});
mapReload();
}
新代码地图加载脚本:
$(function mapReload(){
function chkLeader(val){
if (typeof val ==="undefined"){
x = "No Leader";
}
else {
x = val;
}
return x;
};
markers = [
{latLng: [52.50, 13.39], name: 'Berlin'},
{latLng: [51.5, 0.13], name: 'London'},
{latLng: [48.85, 2], name: 'Paris'}
],
cityData = [
100,
100,
100
]
map = new jvm.WorldMap({
container: $('#map'),
map: 'europe_mill_en',
backgroundColor: '#1C6BA0',
zoomOnScroll: false,
markers: markers,
regionStyle: {
initial: {
fill: '#78B6A4',
}
},
markerStyle: {
initial: {
fill: 'yellow'
}
},
series: {
regions: [{
attribute: 'fill'
}],
markers: [{
attribute: 'r',
scale: [5, 6],
values: cityData
}]
},
onRegionLabelShow: function(event, label, code){
label.html(
'<strong>' +
label.html() +
'</strong>' + '<br>' +
'<strong>' +
'Leader: ' +
'</strong>' +
chkLeader(nationLeaders[code]) + '<br>' +
'<strong>' +
'Population: ' +
'</strong>' +
nationPopulation[code]
);
}
});
map.series.regions[0].setValues(nationColors);
});
但由于某些原因的页面变成空白:(需要
在AJAX调用返回后,你对变量做什么? – Barmar 2014-10-11 02:27:35
基本上,我正在加载一张地图。我想要AJAX调用为地图获取新的变量值并更新它们。我试图把所有的Javascript放入AJAX请求中,并且它有点奏效。但问题是,如果您在另一次AJAX更新之后关闭鼠标,悬停时显示的DIVS仍会显示。 – user3798996 2014-10-11 02:31:25
你只是更新变量。您需要调用适当的地图方法,使其使用新变量更新地图。 – Barmar 2014-10-11 02:33:52