简体   繁体   English

一定时间后停止javascript函数

[英]Stop a javascript function after a certain time

I'm facing quite the challenge here, and since i'm new to web development i don't really know where to look. 我在这里面临相当大的挑战,由于我是Web开发的新手,所以我真的不知道在哪里看。

On this website i'm working on, we are loading a map via a javascript function: 我正在使用的这个网站上,我们正在通过javascript函数加载地图:

initializeMap();

This function is called inside: 此函数在内部称为:

$(document).on("ready", function() {
})

Sometimes this function "initializeMap();" 有时,此函数“ initializeMap();” takes a long time to run, most likely because i'm getting information from another server, and it might timeout if you have a slow connection, or for any other arbitrary reason. 运行时间很长,很可能是因为我正在从另一台服务器获取信息,并且如果连接速度较慢或任何其他任意原因,它可能会超时。

Is there a way to say, if "initializeMap();" 有没有办法说,如果“ initializeMap();” has not completed within 30 seconds, i would like to throw an error (Or console.log for an example). 尚未在30秒内完成,我想抛出一个错误(或以console.log为例)。

I have searched a lot, and "Promise.race" seems to be able to do it, but this system requires old browser support, and we can't really update the node.js on the server, so i'm stuck to ES5. 我已经搜索了很多东西,“ Promise.race”似乎可以做到,但是该系统需要旧的浏览器支持,我们无法真正更新服务器上的node.js,因此我只能使用ES5 。

I hope there are some kind people who can point me in the correct direction. 我希望有些人能指出我正确的方向。

ps. ps。 we use jQuery also (Version 3.1) 我们也使用jQuery(版本3.1)

I found another solution. 我找到了另一个解决方案。

I used setTimeout to check for a loading animation after 30 seconds. 我使用setTimeout在30秒后检查加载的动画。 If the loading animation is still there, i display an error where the user is able to reload the page. 如果加载动画仍然存在,我会显示一个错误,用户可以重新加载页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM