【问题标题】:Wait for ajax data load [duplicate]等待ajax数据加载[重复]
【发布时间】:2021-09-08 08:15:08
【问题描述】:

我有一个从 ajax 调用和视频播放器中提取数据的网格。在将数据加载到网格(DevExtreme.DataGrid)后,我正在尝试播放视频。我该怎么做?

var isLoaded = false;
        $.ajax({
            type: 'GET',
            url: '?handler=GetData&StartTime=' + eventStartTime + '&EndTime=' + eventEndTime,                
            success: function (response) {
                var obj = jQuery.parseJSON(response);
                var _dataGrid = $("#eventGridData").dxDataGrid("instance");
                _dataGrid.option('dataSource', obj);
                _dataGrid.reload();
                _dataGrid.refresh();
                isLoaded = true;
            },
            error: function (error) {
                alert("Error: " + error);
            }
        });
        //this needs to kick in after the data is loaded into the grid and I do a refresh(see above)
        if (isLoaded) {  
            //fetch matching video(by timestamp)
            playbackPlayer.connect({
                address: '127.0.0.1',
                port: '25200',
                timestart: timestart from the first record of the datagrid,
                timeend: timeend from the first record of the datagrid
            });
            playbackPlayer.play();
        }

【问题讨论】:

  • isLoaded = true;行后面添加播放视频的代码
  • 你会接受纯 JavaScript 的答案吗?没有 jQuery(这让我感觉有点恶心)。
  • @ErnestoStifano 自从有了承诺之前,jQuery 就拥有了what amounts to promises$.ajax() 的返回值也完全符合承诺。与香草相比,我真的不明白 jQuery 是如何改变事物的。您只需将 $.ajax 切换为等效的 fetch() 调用 - 结果的实际处理几乎没有什么不同。
  • 如何在纯 JavaScript 中做到这一点?示例代码会有所帮助。我尝试使用 .done 的 ajax 请求,但没有奏效。
  • @SoftwareDveloper 将 if 语句中的代码移动到(或代替)isLoaded = true; 行之后。

标签: javascript ajax load wait


【解决方案1】:

我使用 jQuery 尝试过这种方式,它似乎可以正常工作:

$.when(
$.ajax({
            type: 'GET',
            url: '?handler=GetData&StartTime=' + eventStartTime + '&EndTime=' + eventEndTime,                
            success: function (response) {
                var obj = jQuery.parseJSON(response);
                var _dataGrid = $("#eventGridData").dxDataGrid("instance");
                _dataGrid.option('dataSource', obj);
                _dataGrid.reload();
                _dataGrid.refresh();
                
            },
            error: function (error) {
                alert("Error: " + error);
            }
        })
).then.function(){ 

            //fetch matching video(by timestamp)
            playbackPlayer.connect({
                address: '127.0.0.1',
                port: '25200',
                timestart: timestart from the first record of the datagrid,
                timeend: timeend from the first record of the datagrid
            });
            playbackPlayer.play();
});

只有在网格中有可用数据的情况下,我仍然需要播放视频。我认为 DevExtreme 的数据网格有一个 onLoadingChanged 方法,表明数据已经加载。

【讨论】:

  • $.when() 是多余的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多