使用for循环调用setTimeout(calling setTimeout with a for loop)
我写了一个改变div的css定位的函数
我已成功使用setTimeout以特定间隔调用该函数
现在我要做的是在同一页面上的28个不同的div上调用该函数,每个div都有自己的速度。
我的想法是我可以用for循环这样做:
for (var x = 0; x < 28; x++) { setInterval(function(){changeDirection(divlist[x])}, divs[divlist[x]].speed);}
使用存储速度和id的对象'divs'
唯一可以让他们全部反对计时器的方法是调用setInterval 28次,就像这样......
setInterval(function(){changeDirection(divlist[1])}, divs[divlist[1]].speed); setInterval(function(){changeDirection(divlist[2])}, divs[divlist[2]].speed); setInterval(function(){changeDirection(divlist[3])}, divs[divlist[3]].speed); setInterval(function(){changeDirection(divlist[4])}, divs[divlist[4]].speed);....etc
for循环不起作用......
有没有人知道为什么? 有没有办法用这样的循环在许多不同的函数上调用setInterval
I've written a function that changes the css positioning of a div
I've been successfully using setTimeout to call the function at a specific interval
NOW what I'm trying to do is call the function on 28 different divs on the same page, each with its own speed.
my thinking was that I could do this with a for loop like so:
for (var x = 0; x < 28; x++) { setInterval(function(){changeDirection(divlist[x])}, divs[divlist[x]].speed);}
using the object 'divs' where the speed and id are stored
the only way I could get them all to move against the timer was to call setInterval 28 times like so...
setInterval(function(){changeDirection(divlist[1])}, divs[divlist[1]].speed); setInterval(function(){changeDirection(divlist[2])}, divs[divlist[2]].speed); setInterval(function(){changeDirection(divlist[3])}, divs[divlist[3]].speed); setInterval(function(){changeDirection(divlist[4])}, divs[divlist[4]].speed);....etc
the for loop did NOT work...
does anyone have any idea why? and is there a way to call setInterval on many different functions with a loop like this
原文:https://stackoverflow.com/questions/10020127
最满意答案
只需替换此代码即可
var marker = new google.maps.Marker({ position: point, map: map, icon: image, });
对此
var marker = new google.maps.Marker({ position: point, map: map, icon: image });
(第509行)
Just replace this code
var marker = new google.maps.Marker({ position: point, map: map, icon: image, });
to this
var marker = new google.maps.Marker({ position: point, map: map, icon: image });
(line 509)
相关问答
更多-
让Google App Engine和Google Maps API v3畅谈(Getting Google App Engine and Google Maps API v3 talking)[2023-05-02]
我不确定您是否在询问如何在数据存储中存储/加载Lat / Lng数据,或者如果您问我如何将数据导入javascript,那么我将回答这两个问题。 假设您正在使用原始数据存储区,只需创建一个GeoPtProperty来存储数据。 从datstore加载对象时,您将拥有一个具有lat和lon属性的GeoPt对象的实例。 至于如何将这些数据转换为javascript,您有多种选择,具体取决于您的使用案例,不同的选项对您来说可能更好。 1.渲染页面的其余部分时,可以将数据输出到模板。 它的工作原理将根据您的模板系统 ... -
在v3 API的早期阶段, getBounds()方法需要地图图块才能完成加载,以返回正确的结果。 但是现在看来,您可以收听bounds_changed事件,甚至在tilesloaded事件之前也会触发:
Google Maps v3 - getBounds is undefine ... -
我能够通过在初始化中移动cmarkers代码来解决这个问题。 我认为javascript并没有说清楚initialize中的map变量是cmarkers函数中的map变量(对不起,如果语言不精确,我在js中不是很好)。 见下文: function initialize() { var rendererOptions = { draggable: true, panel:document.getElementById('dir ...
-
只需替换此代码即可 var marker = new google.maps.Marker({ position: point, map: map, icon: image, }); 对此 var marker = new google.maps.Marker({ position: point, map: map, icon: image }); (第509行) Just replace this code var m ...
-
更新 截至2011年3月 现在,所有开发人员都可以通过https访问Maps API v3,静态地图API和Maps API Web服务: 这是Google Maps的一个重大问题 。 如果您想通过https访问Google地图JavaScript,则必须注册为Google地图专业版客户 。 关于该主题的Google Maps API问题出现了许多哗众取宠的评论,但Google的最后一句话是他们不会在免费API中支持这一功能。 我最近几天花了一个项目移植到Bing Maps (它确实支持https),这个问 ...
-
navigator.geolocation.getCurrentPosition()是异步的。 像这样重新组织你的代码: var mapOptions = { zoom: 8, mapTypeId: google.maps.MapTypeId.HYBRID } function initialize() { // Check if user support geo-location if (navigator.geolocation) { navigator ...
-
根据文档,你根本无法传递字符串。 你必须明确地拆分这两个部分并将它们作为数字传递: var gpsPos = '50.082243,24.302628'; var splitted = gpsPos.split(","); var myLatlng = new google.maps.LatLng(splitted[0] - 0, splitted[1] - 0); // '- 0' will automatically make it a number According to the docs yo ...
-
在加载之前,您无法使用Google Maps Javascript API v3。 您的标记创建在加载API之前正在运行。 您需要将其移入initialize函数,该函数在API可用之前不会执行。