[英]Delay liquid page render until after Ajax returns data
我正在使用Liquid,并且可以访问服务器和客户端。 我想一次获取用户位置,然后根据用户位置渲染不同的液体元素。 因此,隐藏div无效。 实际上,我需要将渲染延迟大约半秒钟,直到通过Ajax返回国家代码为止。 然后我可以从那里拿走。 我尝试了一下,但是没有运气,它没有延迟页面渲染,只是延迟了将消息记录到控制台。
<script>
$(window).load(function () {
setTimeout(function(){ console.log("waiting 2 secs..");
},2000); // set the time here
});
jQuery.ajax( {
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function(location) {
{% assign user_country = location.country_code %}
console.log("Hey this is the country code " + location.country_code);
}
});
</script>
您不应该使用jsonp
因为这不是最佳实践。
正如Sam指出的那样,您可以使用done()
:
jQuery.ajax({
url: '//freegeoip.net/json/',
type: 'POST',
async: false,
success: function(location) {
{% assign user_country = location.country_code %}
console.log("Hey this is the country code " + location.country_code);
}
}).done(function() {
// CODE HERE WILL ONLY RUN ONCE AJAX REQUEST IS COMPLETED
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.