有没有办法延迟 popstate 事件?
Is there a way to delay a popstate event?
我已经尝试了一些方法来查看是否可以创建一个跨浏览器解决方案来延迟 popstate 事件,但没有成功。
有人有什么想法或想法吗?
下面显然行不通,但大意是:
$(window).on('popstate', function(e) {
// something here to delay the history pageload
console.log('a wild console has appeared!');
});
因此流程将遵循以下顺序:
- 点击了浏览器 "back" 或 "forward" 按钮
- 运行初始代码
- 页面更改前的延迟
- 页面变化
根据Documentation popstate 事件是
only triggered by doing a browser action such as a click on the back button
所以我不相信它会在用户点击时被触发 'forward'(在某些浏览器中会有所不同)
全文供参考:
The popstate event is fired when the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.
Note that just calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by doing a browser action such as a click on the back button (or calling history.back() in JavaScript).
Browsers tend to handle the popstate event differently on page load. Chrome (prior to v34) and Safari always emit a popstate event on page load, but Firefox doesn't.
上面的更新答案
您的代码有效。 (见下文)-我添加了一个元素来触发事件(你可以在控制台看到结果)
确保在使用前 jQuery 库包含在 HTML 中。
$(window).on('popstate', function(e) {
console.log('fired instantly!');
var timer = setTimeout(function() {
console.log('delayed popstate!');
clearTimeout(timer);
}, 1000);
});
$(window).trigger( 'popstate') ;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
我已经尝试了一些方法来查看是否可以创建一个跨浏览器解决方案来延迟 popstate 事件,但没有成功。
有人有什么想法或想法吗?
下面显然行不通,但大意是:
$(window).on('popstate', function(e) {
// something here to delay the history pageload
console.log('a wild console has appeared!');
});
因此流程将遵循以下顺序:
- 点击了浏览器 "back" 或 "forward" 按钮
- 运行初始代码
- 页面更改前的延迟
- 页面变化
根据Documentation popstate 事件是
only triggered by doing a browser action such as a click on the back button
所以我不相信它会在用户点击时被触发 'forward'(在某些浏览器中会有所不同)
全文供参考:
The popstate event is fired when the active history entry changes. If the history entry being activated was created by a call to history.pushState() or was affected by a call to history.replaceState(), the popstate event's state property contains a copy of the history entry's state object.
Note that just calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by doing a browser action such as a click on the back button (or calling history.back() in JavaScript).
Browsers tend to handle the popstate event differently on page load. Chrome (prior to v34) and Safari always emit a popstate event on page load, but Firefox doesn't.
上面的更新答案
您的代码有效。 (见下文)-我添加了一个元素来触发事件(你可以在控制台看到结果)
确保在使用前 jQuery 库包含在 HTML 中。
$(window).on('popstate', function(e) {
console.log('fired instantly!');
var timer = setTimeout(function() {
console.log('delayed popstate!');
clearTimeout(timer);
}, 1000);
});
$(window).trigger( 'popstate') ;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>