如何停止javascript等待延迟完成?

编程入门 行业动态 更新时间:2024-10-25 04:24:22
本文介绍了如何停止javascript等待延迟完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个函数,它带有一个参数并进行同步加载.

I have a function which takes a parameter and does synchronous loadings.

我将其更改为执行异步jQuery.ajax调用以检索数据并调用回调.旧代码(无法修改)使用相同的功能,但失败.我需要能够以两种方式检索数据,以便异步代码可以继续运行,而旧的同步代码可以检索缓存的数据(如果异步已加载该数据,同步加载该数据或等待已运行的任何异步请求).

I changed it to do an asynchronous jQuery.ajax call to retrieve data and call a callback. Old code (which cannot be modified) uses the same function and fails. I need to be able to retrieve the data both ways so async code can keep on running and old sync code can retrieve the cached data if async already loaded it, load it synchronously or wait for any async requests which are already running.

var ajaxQueries = {} loadEngineData(fileId, callback) { var sync = callback == undefined; if (ajaxQueries[fileId]) { if (sync) { //Need to stop here and wait for ajaxQueries[fileId] to finish //Execution cannot continue! If we return before defered has ended here 3rd party scripts will fail. return data; } else { ajaxQueries[fileId].done(function(data){callback(data)}); } } else { ajaxQueries[fileId] = $.ajax({ async:!sync, type:'GET', url:fileId2Name(fileId), data:null, dataType:'text', }); if (sync) { var _data = undefined; ajaxQueries[fileId].done(function(data){_data=data}); return _data; } ajaxQueries[fileId].done(function(data){callback(data);}); } }

如果旧脚本调用该函数,则ajax查询将同步运行,并且所有后续调用将使用延迟的.done和.fail,一切都会好起来.

If an old script calls the function the ajax query will run synchronously and all subsequent calls will use the deferred .done and .fail and everything will be fine.

var enginePhyData = loadEngineData('physics'); //Do stuff

如果新脚本首先调用该函数,并且在此加载过程中旧脚本试图从同一文件加载数据,那么我将遇到以下情况:ajaxQueries[fileId]中的延迟ajax查询正在异步运行,因此我必须等待完成加载数据以避免破坏现有的库.

If a new script calls the function first and during this loading an old script tries to load data from the same file, I end with a situation where the deferred ajax query in ajaxQueries[fileId] is running asynchronously and I have to wait for it to finish loading the data to avoid breaking the existing libs.

我简化了loadEngineData以使问题集中.否则,它会更复杂一些,并允许调用要并行加载的文件列表

I simplified loadEngineData to keep the question focussed. Otherwise it's a bit more complex and allows calling a list of files to load in parallel

loadEngineData(['geometry', 'scripts'], function() { var phy = loadEngineData('physics'); var geo = loadEngineData('geometry'); var scr = loadEngineData('scripts'); //run the async function; }); loadEngineData('physics', function() { //run the async function; }); //... //in the scripts file var phy = loadEngineData('physics'); //here phy might be undefined if this file went through eval() before `loadEngineData('physics',...)` was called;

我可以使代码现代化,但是问题是某些文件在项目之间共享并在不同的环境中运行.我目前无法在不分叉依赖项的情况下修改旧脚本.

I could modernize the code, but the issue is that some files are shared across projects and run in different environments. I can't currently modify the old scripts without forking the dependencies.

推荐答案

在等待一些异步ajax调用完成时,您无法停止javascript的执行. Javascript根本无法正常工作.

You cannot stop the execution of javascript while waiting for some asynchronous ajax call to finish. Javascript simply doesn't work that way.

相反,您必须重新编写程序,以便要等待异步ajax调用完成的代码将通过回调函数执行,而不是在ajax调用执行后同步执行.

Instead, you have to rework the way you program so that code that wants to wait for an asychronous ajax call to be completed will execute via a callback function, not synchronously after the ajax call executes.

更多推荐

如何停止javascript等待延迟完成?

本文发布于:2023-11-12 18:50:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!