Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以用javascript缓存一段HTML吗? 我的设想:_Javascript_Asp.net_Vb.net_Caching_Webforms - Fatal编程技术网

我可以用javascript缓存一段HTML吗? 我的设想:

我可以用javascript缓存一段HTML吗? 我的设想:,javascript,asp.net,vb.net,caching,webforms,Javascript,Asp.net,Vb.net,Caching,Webforms,我有一个页面,我们称之为items.aspx,它有一个HTML表,该表是通过对我的ASP Web API控制器的$.get()调用生成的。当用户单击HTML表中的一个项目时,该用户将被发送到表示该项目详细信息的页面,我们称之为itemDetails.aspx。我预测查看itemDetails.aspx页面,然后决定取消并重定向回items.aspx页面将是常见的 问题是: 问题是,每次加载items.aspx页面时,ajax调用都会再次运行,填充表需要几秒钟的时间 问题是: 当用户单击项以转到i

我有一个页面,我们称之为items.aspx,它有一个HTML表,该表是通过对我的ASP Web API控制器的
$.get()
调用生成的。当用户单击HTML表中的一个项目时,该用户将被发送到表示该项目详细信息的页面,我们称之为itemDetails.aspx。我预测查看itemDetails.aspx页面,然后决定取消并重定向回items.aspx页面将是常见的

问题是: 问题是,每次加载items.aspx页面时,ajax调用都会再次运行,填充表需要几秒钟的时间

问题是:
当用户单击项以转到itemDetails.aspx页面时,是否可以在有限的时间内存储/缓存表的HTML输出?因为我只希望在用户单击项目链接时发生这种情况,所以我认为javascript将是最好的工具。

您可以使用一个函数返回HTML的jQuery,无论它是从本地缓存还是从AJAX请求加载的

一个简单的例子:

// Refers to cached HTML when available
var cached;

// Returns a promise for the HTML
function getHtml() {
  var defer;
  if(cached) {
    defer = new Deferred();
    defer.resolve(cached);
  } else {
    defer = $.get(/* ... */);
    defer.then(function(response) {
      cached = response;
    })
  }

  return defer.promise();
}
用法:

getHtml().then(function(html) {
  $container.html(html);
})

真正的实现可能会阻止并发的
$。获取
调用,并在需要时使缓存过期。

这肯定是一个正确的答案,但我认为我们最终解决的解决方案是将JSON数据存储在window.localstorage中。正是JSON数据的加载导致了等待时间。无需缓存所有HTML。此函数将不受影响地工作-只需调用它
getJson
:)