 |
ต้องการให้ JavaScript หน่วงเวลาไว้ 3 วิก่อนทำลูปต่อไป แต่กลายเป็นว่า รอ 3 วิแล้วทำรวดเดียว ช่วยหน่อยครับ |
|
 |
|
|
 |
 |
|
สมมุติว่าต้องการใส่จุด 10 จุดบนแผนที่ครับ โดยจะเห็นว่าจุดแต่ละจุดค่อยๆใส่ลงไปทีละจุดบนแผนที่
ผมใช้ SetTimeout หน่วงเวลาใน forลูป ไว้ 3 วิ แต่เมื่อรันดูกลายเป็นว่า รอ 3 วิ แล้วจุดก็ใส่รวดเดียวเลยครับ -,-"
ผิดพลาดตรงไหนช่วยหน่อยครับ
ลองรันดูครับ
Code (JavaScript)
var map;
var marker;
var markersArr = [];
//Load Google Map
function initialize() {
var latlng = new google.maps.LatLng(13.7031333333333, 100.518616666667);
var myOptions ={
zoom: 12,
center: latlng,
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
markersArr[0] = new google.maps.LatLng(13.771095, 100.530201666667);
markersArr[1] = new google.maps.LatLng(13.76914, 100.528776666667);
markersArr[2] = new google.maps.LatLng(13.73082, 100.517566666667);
markersArr[3] = new google.maps.LatLng(13.7273633333333, 100.517475);
markersArr[4] = new google.maps.LatLng(13.7145966666667, 100.52008);
markersArr[5] = new google.maps.LatLng(13.7031333333333, 100.518616666667);
markersArr[6] = new google.maps.LatLng(13.7012833333333, 100.519271666667);
markersArr[7] = new google.maps.LatLng(13.6950733333333, 100.525245);
markersArr[8] = new google.maps.LatLng(13.692845, 100.526658333333);
markersArr[9] = new google.maps.LatLng(13.692045, 100.526266666667);
for (i in markersArr) {
//ต้องการให้หน่วงเวลาไว้ 3 วิ ก่อนใส่จุดต่อไป แต่กลายเป็นว่ารอ 3 วิ แล้วใส่จุดรวดเดียว 10 จุด
setTimeout("addMarker(markersArr[" + i + "])", 3000);
}
}
// add จุด
function addMarker(mkr) {
marker = new google.maps.Marker({ position: mkr, map: map, title: "" + mkr });
}
Tag : JavaScript
|
|
 |
 |
 |
 |
Date :
2011-02-01 11:16:40 |
By :
stricken |
View :
3957 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ
แต่ใช้วิธีเขียนฟังชั้นให้จิ้มจุดเดียว แล้วค่อยใช้ setInterval จัดเวลาเอาอะครับ
|
 |
 |
 |
 |
Date :
2011-02-02 11:52:04 |
By :
stricken |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สอบถามเรื่องการหน่วงเวลาของจุด Marker ด้วยครับ
คือว่าตอนนี้แสดงจุด Marker จากดาต้าเบสได้แล้วครับ แต่ว่ามันจะแสดงทุกจุดพร้อมกัน
อยากทำให้มันแสดงทีละจุด ทุก 3 วินาทีครับ
ลองค้นดูแล้วมีคนให้ใช้ฟังชั่น setInterval(function() {
// Do something every 2 seconds
}, 2000);
คือผมนำมาใช้ไม่เป็นครับ ไม่รู้จะเขียนแบบไหน แทรกตรงไหน รบกวนผู้รู้ด้วยนะครับ
Code (ASP)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Map.aspx.cs" Inherits="GoogleMap.Map" %>
<%--A sample project by Ghaffar khan--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Your Data on Google Map </title>
<%--Google API reference--%>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false
&key="Z3UlwJVUBDAReP3ijQ4eNap9FznZ9qIYzIv7pbaN4v4" type="text/javascript">
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">
<%--Place holder to fill with javascript by server side code--%>
<asp:Literal ID="js" runat="server"></asp:Literal>
<%--Place for google to show your MAP--%>
<div ID="map_canvas" style="height: auto; position: absolute; bottom:0; left:0; right:0; top:0;">
</div>
<br />
</asp:Panel>
<br />
</form>
</body>
</html>
Code (C#)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace GoogleMap
{
// A sample project by Ghaffar khan
public partial class Map : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet1TableAdapters.LocationsTableAdapter da = new GoogleMap.DataSet1TableAdapters.LocationsTableAdapter();
DataSet1.LocationsDataTable table = new DataSet1.LocationsDataTable();
da.Fill(table);
BuildScript(table);
}
private void BuildScript(DataTable tbl)
{
String Locations = "";
foreach (DataRow r in tbl.Rows)
{
// bypass empty rows
if (r["Latitude"].ToString().Trim().Length == 0)
continue;
string Latitude = r["Latitude"].ToString();
string Longitude = r["Longitude"].ToString();
// create a line of JavaScript for marker on map for this record
Locations += Environment.NewLine + " map.addOverlay(new GMarker(new GLatLng(" + Latitude + "," + Longitude + ")));";
}
Code (JavaScript)
js.Text = @"<script type='text/javascript'>
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('map_canvas'));
map.setCenter(new GLatLng(13.721658,100.453184), 12);
" + Locations + @"
map.setUIToDefault();
}
}
</script> ";
}
}
}
|
 |
 |
 |
 |
Date :
2012-02-09 13:02:59 |
By :
autorun.exe |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|