首页 \ 问答 \ 使用for循环调用setTimeout(calling setTimeout with a for loop)

使用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
更新时间:2024-03-15 16:03

最满意答案

只需替换此代码即可

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)

相关问答

更多
  • 我不确定您是否在询问如何在数据存储中存储/加载Lat / Lng数据,或者如果您问我如何将数据导入javascript,那么我将回答这两个问题。 假设您正在使用原始数据存储区,只需创建一个GeoPtProperty来存储数据。 从datstore加载对象时,您将拥有一个具有lat和lon属性的GeoPt对象的实例。 至于如何将这些数据转换为javascript,您有多种选择,具体取决于您的使用案例,不同的选项对您来说可能更好。 1.渲染页面的其余部分时,可以将数据输出到模板。 它的工作原理将根据您的模板系统 ...
  • 在v3 API的早期阶段, getBounds()方法需要地图图块才能完成加载,以返回正确的结果。 但是现在看来,您可以收听bounds_changed事件,甚至在tilesloaded事件之前也会触发: Google Maps v3 - getBounds is undefine ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/googlemapsapiv3bjszhjz_307" target="_blank">Google Maps API v3标记有时只会加载(Google Maps API v3 Markers only load sometimes)</a><i>[2021-12-28] </i></h2> </div> <div class="tw_li_cont"> 我能够通过在初始化中移动cmarkers代码来解决这个问题。 我认为javascript并没有说清楚initialize中的map变量是cmarkers函数中的map变量(对不起,如果语言不精确,我在js中不是很好)。 见下文: function initialize() { var rendererOptions = { draggable: true, panel:document.getElementById('dir ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/ie6bujiagooglemapsapiv3_331" target="_blank">IE6:不加载Google Maps API v3(IE6: Doesn't load Google Maps API v3)</a><i>[2023-11-13] </i></h2> </div> <div class="tw_li_cont"> 只需替换此代码即可 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 ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/googlemapsapiv3tonghttps_366" target="_blank">Google Maps API v3通过HTTPS?(Google Maps API v3 over HTTPS?)</a><i>[2023-08-19] </i></h2> </div> <div class="tw_li_cont"> 更新 截至2011年3月 现在,所有开发人员都可以通过https访问Maps API v3,静态地图API和Maps API Web服务: 这是Google Maps的一个重大问题 。 如果您想通过https访问Google地图JavaScript,则必须注册为Google地图专业版客户 。 关于该主题的Google Maps API问题出现了许多哗众取宠的评论,但Google的最后一句话是他们不会在免费API中支持这一功能。 我最近几天花了一个项目移植到Bing Maps (它确实支持https),这个问 ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/googlemapsapiv3tjgxbjaddingupdat_376" target="_blank">Google Maps API(v3)添加/更新标记(Google Maps API (v3) adding/updating markers)</a><i>[2023-02-18] </i></h2> </div> <div class="tw_li_cont"> navigator.geolocation.getCurrentPosition()是异步的。 像这样重新组织你的代码: var mapOptions = { zoom: 8, mapTypeId: google.maps.MapTypeId.HYBRID } function initialize() { // Check if user support geo-location if (navigator.geolocation) { navigator ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/googlemapsapiv3qiguaicuowu_431" target="_blank">Google Maps API v3是一个非常奇怪的错误?(A very strange bug with Google Maps API v3?)</a><i>[2022-03-29] </i></h2> </div> <div class="tw_li_cont"> 根据文档,你根本无法传递字符串。 你必须明确地拆分这两个部分并将它们作为数字传递: 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 ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/googlemapsapiv3biaoji_454" target="_blank">Google Maps Api v3标记。(Google Maps Api v3 marker. google is undefined)</a><i>[2024-02-03] </i></h2> </div> <div class="tw_li_cont"> 在加载之前,您无法使用Google Maps Javascript API v3。 您的标记创建在加载API之前正在运行。 您需要将其移入initialize函数,该函数在API可用之前不会执行。 <script> function initialize() { var mapOptions = { zoom: 10, center: new google.maps.LatLng(51.817116, 4.780616), mapTypeId: google.maps.Map ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/ginfowindowtabdxygooglemapsapiv3_461" target="_blank">GInfoWindowTab等效于Google Maps API v3(GInfoWindowTab equivalent in Google Maps API v3)</a><i>[2023-01-14] </i></h2> </div> <div class="tw_li_cont"> InfoBubble实用程序库(位于Maps API V3之上的库)提供此功能。 请参阅: http : //google-maps-utility-library-v3.googlecode.com/svn/trunk/infobubble/examples/example.html The InfoBubble utility library (a library that sits on top of Maps API V3) provides this functionality. See: htt ... </div> </div> </li> <li class="tw_li clearfix"> <div class="tw_li_con"> <div class="tw_li_title"> <h2><a href="/wenda/internetexplorerzgooglemapsapiv3_474" target="_blank">Internet Explorer 9中的Google Maps API v3无法加载(Google Maps API v3 in internet explorer 9 doesn't load)</a><i>[2022-12-19] </i></h2> </div> <div class="tw_li_cont"> 您已为兼容模式添加了元标记,并将其设置为IE7。 将其设置为IE8 You have added the meta-tag for compatibility-mode and set it to IE7. Set it to IE8 </div> </div> </li> </ul> </div> <div class="main_right"> <div class="search-out"> <div class="search"> <form action="/wenda" target="_blank" method="get"> <input type="search" autocorrect="off" autocomplete="off" placeholder="请输入关键词" id="q" name="q" value=""> <button class="btn_s" type="submit">搜索</button> </form> </div> </div> <div class="commonh"> <h2>相关文章</h2> <span class="fr"><a href="/jiaocheng" target="_blank">更多</a></span> </div> <div class="right_list"> <li> <a title="用A标签实现页面内容定位 点击链接跳到具体位置" href="/article/yAbqsxymnrdwdjljtdjtwz_2" target="_blank">用A标签实现页面内容定位 点击链接跳到具体位置</a> </li> <li> <a title="[译文] 恶意软件行为综述 - A View on Current Malware Behaviors" href="/article/yweyrjxwzsAViewonCurrentMalwareBehaviors_3" target="_blank">[译文] 恶意软件行为综述 - A View on Current Malware Behaviors</a> </li> <li> <a title="Scaling Pinterest - From 0 To 10s Of Billions Of Page Views A Month In Two Years" href="/article/ScalingPinterestFrom0To10sOfBill_2" target="_blank">Scaling Pinterest - From 0 To 10s Of Billions Of Page Views A Month In Two Years</a> </li> <li> <a title="[转]So You Want To Be A Producer" href="/article/zhuanSoYouWantToBeAProducer_3" target="_blank">[转]So You Want To Be A Producer</a> </li> <li> <a title="Becoming a data scientist" href="/article/Becomingadatascientist_2" target="_blank">Becoming a data scientist</a> </li> <li> <a title="trouble is a friend" href="/article/troubleisafriend_3" target="_blank">trouble is a friend</a> </li> <li> <a title="Fading Like a Flower - Roxette" href="/article/FadingLikeaFlowerRoxette_3" target="_blank">Fading Like a Flower - Roxette</a> </li> <li> <a title="<a>按钮样式" href="/article/aanniuyangshi_3" target="_blank"><a>按钮样式</a> </li> <li> <a title="She’s Not Carrying A Handbag" href="/article/ShesNotCarryingAHandbag_3" target="_blank">She’s Not Carrying A Handbag</a> </li> <li> <a title="a favorite song" href="/article/afavoritesong_3" target="_blank">a favorite song</a> </li> </div> <div class="commonh"> <h2>最新问答</h2> <span class="fr"><a href="/wenda" target="_blank">更多</a></span> </div> <div class="right_list"> <li> <a title="获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)" href="/wenda/huoqumvcdisplaymodehouzhui_283" target="_blank">获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)</a> </li> <li> <a title="如何通过引用返回对象?(How is returning an object by reference possible?)" href="/wenda/yyfhdx_312" target="_blank">如何通过引用返回对象?(How is returning an object by reference possible?)</a> </li> <li> <a title="矩阵如何存储在内存中?(How are matrices stored in memory?)" href="/wenda/juzhencunchuzaineizhong_536" target="_blank">矩阵如何存储在内存中?(How are matrices stored in memory?)</a> </li> <li> <a title="每个请求的Java新会话?(Java New Session For Each Request?)" href="/wenda/qingqiujavaxinhui_485" target="_blank">每个请求的Java新会话?(Java New Session For Each Request?)</a> </li> <li> <a title="css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)" href="/wenda/cssfddivzzdbth1_439" target="_blank">css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)</a> </li> <li> <a title="无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)" href="/wenda/tuxiangcaffeyuceyilei_557" target="_blank">无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)</a> </li> <li> <a title="xcode语法颜色编码解释?(xcode syntax color coding explained?)" href="/wenda/xcodeyufayansebianmajieshi_364" target="_blank">xcode语法颜色编码解释?(xcode syntax color coding explained?)</a> </li> <li> <a title="在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)" href="/wenda/access2010runtimezsoffice2000xdg_422" target="_blank">在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)</a> </li> <li> <a title="从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)" href="/wenda/ddwebzjtxcsfwq_528" target="_blank">从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)</a> </li> <li> <a title="从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))" href="/wenda/jbbfzwjbljxbb_417" target="_blank">从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))</a> </li> <li> <a title="西安哪有PLC可控制编程的培训" href="/wenda/xanyplckkbcpx_36" target="_blank">西安哪有PLC可控制编程的培训</a> </li> <li> <a title="在Entity Framework中选择基类(Select base class in Entity Framework)" href="/wenda/entityframeworkzhongxuanjilei_415" target="_blank">在Entity Framework中选择基类(Select base class in Entity Framework)</a> </li> <li> <a title="在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)" href="/wenda/androidzccwsjjxrqbwnullslxl_307" target="_blank">在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)</a> </li> <li> <a title="电脑二级VF有什么用" href="/wenda/diannaoerjivfshenmeyong_16" target="_blank">电脑二级VF有什么用</a> </li> <li> <a title="Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)" href="/wenda/datamapperrubytianjiahookfangfa_475" target="_blank">Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)</a> </li> <li> <a title="金华英语角." href="/wenda/jinhuayingyujiao_31" target="_blank">金华英语角.</a> </li> <li> <a title="手机软件如何制作" href="/wenda/sjrjzz_103" target="_blank">手机软件如何制作</a> </li> <li> <a title="用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)" href="/wenda/yyandroidwebviewztxbcsxwcd_150" target="_blank">用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)</a> </li> <li> <a title="注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)" href="/wenda/wdypylphp_555" target="_blank">注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)</a> </li> <li> <a title="如何读R中的大数据集[复制](How to read large dataset in R [duplicate])" href="/wenda/durzhongdashujujifuzhi_350" target="_blank">如何读R中的大数据集[复制](How to read large dataset in R [duplicate])</a> </li> <li> <a title="Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)" href="/wenda/unityheighmapdxkddxzdfblgx_375" target="_blank">Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)</a> </li> <li> <a title="如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)" href="/wenda/tzpipedoutputstreamxcxrygzpipedi_163" target="_blank">如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)</a> </li> <li> <a title="python的访问器方法有哪些" href="/wenda/pythonfangwenqifangfa_93" target="_blank">python的访问器方法有哪些</a> </li> <li> <a title="DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)" href="/wenda/devicenetworkinformation_95" target="_blank">DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)</a> </li> <li> <a title="在Ruby中对组合进行排序(Sorting a combination in Ruby)" href="/wenda/rubyzhongzuhepaixu_148" target="_blank">在Ruby中对组合进行排序(Sorting a combination in Ruby)</a> </li> <li> <a title="网站开发的流程?" href="/wenda/wangzhankaifadeliucheng_75" target="_blank">网站开发的流程?</a> </li> <li> <a title="使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)" href="/wenda/zendframeworkzjoinsqljssj_199" target="_blank">使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)</a> </li> <li> <a title="条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)" href="/wenda/tdgslxgsmsbhgz_207" target="_blank">条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)</a> </li> <li> <a title="透明度错误IE11(Transparency bug IE11)" href="/wenda/toumingducuowuie11_313" target="_blank">透明度错误IE11(Transparency bug IE11)</a> </li> <li> <a title="linux的基本操作命令。。。" href="/wenda/linuxdejibencaozuomingling_66" target="_blank">linux的基本操作命令。。。</a> </li> </div> </div> </div> </div> <div style="clear:both;"></div> <div class="footer"> <div class="mainbox"> <div class="info"> <p>Copyright ©2023 <a href="https://www.peixunduo.com" target="_blank">peixunduo.com</a> All Rights Reserved.<a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备14003112号</a> </p> <p>本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!</p> </div> </div> </div> <script type="text/javascript" src="/resources/js/common.js?v=324"></script> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https'){ bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else{ bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?9eebaceb5e4371a0aad59712a1a1ecff"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>