为了遵循Google Page Insights的建议,我得到了“ JS渲染块”建议,该建议与jQuery主文件有关。
我的网站使用WordPress和一些插件。其中一个插件将其JS内联。因此,当我移动jQuery到页脚中进行加载时,或者使用“ defer”模式进行加载时,jQuery is not defined内联代码都会触发。
我试图找到一种全局解决方案来“捕获”所有内联脚本,并jQuery在文档末尾执行主文件后将其延迟。
我写了一个非常适合我的解决方案。对于我的特定情况,这是非常简单的解决方案,但是我无法将其作为过滤器the_content或小部件输出。我想使它成为一个全局解决方案,所以我不必担心任何地方会触发JS。
有什么想法使它起作用吗?到目前为止,这里是我针对此特定情况的代码,它通过短代码运行:
/* Get shortcode HTML */
$widget_shortcode = do_shortcode($shortcode);
/* Take out all scripts into an array */
$delayed_scripts = array();
preg_match_all('#<script(.*?)</script>#is', $widget_shortcode, $match);
foreach ($match as $val){
$delayed_script = '<script '.$val[0].'</script>';
array_push($delayed_scripts, $delayed_script);
}
/* Remove all scripts from HTML */
$widget_shortcode = preg_replace('#<script(.*?)</script>#is', '', $widget_shortcode);
/* Echo filtered HTML */
echo $widget_shortcode;
Run Code Online (Sandbox Code Playgroud)
并且在结束</body>标记之前:
foreach ($delayed_scripts …Run Code Online (Sandbox Code Playgroud) 例如,我发现了一些基于 promise 的 api 库,我需要在某个时间间隔内使用该库发出 api 请求,无限次(如通常的后端循环)。这个 api 请求 - 实际上是承诺链。
所以,如果我写这样的函数:
function r(){
return api
.call(api.anotherCall)
.then(api.anotherCall)
.then(api.anotherCall)
...
.then(r)
}
Run Code Online (Sandbox Code Playgroud)
会导致堆栈溢出吗?
我想出的解决方案是使用 setTimeout 进行r递归调用。
function r(){
return api
.call(api.anotherCall)
.then(api.anotherCall)
.then(api.anotherCall)
.then(()=>{setTimeout(r, 0)})
}
Run Code Online (Sandbox Code Playgroud)
所以 setTimeoutr只会在调用栈为空时才会调用。
这是好的解决方案,还是有一些递归调用承诺的标准方法?
我需要在<meta name="apple-itunes-app" content="app-id=xxxxx">特定页面上添加一个元标记(特别是),但是我们的模板设置方式,我不可能直接编辑HEAD标记的代码(对于公司,而不是技术,原因).
因此,有没有办法在BODY标记中使用JQuery来添加这个元标记?
根据 Apple Swift 文档defer
无论执行如何离开当前代码块,无论它是因为抛出错误还是因为诸如 return 或 break 之类的语句而离开,此语句都允许您执行任何应该执行的必要清理。
但是这段代码:
enum SomeError: ErrorType {
case BadLuck
}
func unluckey() throws {
print("\n\tunluckey() -> someone will have a bad day ;)\n")
throw SomeError.BadLuck
}
func callsUnluckey() throws {
print("callsUnluckey() -> OPENING something")
defer {
print("callsUnluckey() -> CLOSEING something")
}
print("callsUnluckey() -> WORKING with something")
try unluckey()
print("callsUnluckey() -> will never get here so chill...")
defer {
print("callsUnluckey() -> why this is not getting called?")
}
}
do {
try …Run Code Online (Sandbox Code Playgroud) 我正在尝试降低页面速度,因此在两个外部 Javascript 中都尝试了“延迟归因”。但是,当我使用延迟归因时,我的主网站的视频剪辑消失了。
请让我知道如何解决这个问题。
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="SemiColonWeb" />
<!-- Stylesheets
============================================= -->
<link href="http://fonts.googleapis.com/earlyaccess/jejugothic.css" rel="stylesheet" type="text/css" />
<link href="http://fonts.googleapis.com/css?family=Lato:300,400,400italic,600,700|Raleway:300,400,500,600,700|Crete+Round:400italic" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="style.css" type="text/css" />
<link rel="stylesheet" href="css/dark.css" type="text/css" />
<link rel="stylesheet" href="css/font-icons.css" type="text/css" />
<link rel="stylesheet" href="css/animate.css" type="text/css" />
<link rel="stylesheet" href="css/magnific-popup.css" type="text/css" />
<link rel="stylesheet" href="css/responsive.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<!--[if lt IE 9]>
<script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script> …Run Code Online (Sandbox Code Playgroud) 我想在进入另一个循环之前先完成所有结果。我怎样才能做到这一点?
function callback_Original(results, status) {
if (status === google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createMarker_Original(results[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
有时它总是给出很少的位置。
function createMarker_Original(place) {
var photos = place.photos;
if (!photos) {
return;
}
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('<div style="float:left"><img src="' + photos[0].getUrl({'maxWidth': 120, 'maxHeight': 120})
+ '"></div><div style="float:right; padding: 10px;"><b>Name: </b>'+ place.name +'<br/>'+
'<b>Coordinates : </b>'+ place.geometry.location +'<br/>'+
'<b>Type: </b>'+ type +'<br/>'); …Run Code Online (Sandbox Code Playgroud) 我有一个可以取消的蓝鸟承诺.取消后,我必须做一些工作来巧妙地中止正在运行的任务.可以通过两种方式取消任务:通过promise.cancel()或promise.timeout(delay).
为了能够在取消或超时时巧妙地中止任务,我必须捕获CancellationErrors和TimeoutErrors.捕获CancellationError有效,但由于某种原因我无法捕获TimeoutError:
var Promise = require('bluebird');
function task() {
return new Promise(function (resolve, reject) {
// ... a long running task ...
})
.cancellable()
.catch(Promise.CancellationError, function(error) {
// ... must neatly abort the task ...
console.log('Task cancelled', error);
})
.catch(Promise.TimeoutError, function(error) {
// ... must neatly abort the task ...
console.log('Task timed out', error);
});
}
var promise = task();
//promise.cancel(); // this works fine, CancellationError is caught
promise.timeout(1000); // PROBLEM: this TimeoutError isn't caught!
Run Code Online (Sandbox Code Playgroud)
如何 …
我和我的朋友们正在PyGame中做一个问答游戏,想知道如何,当用户按下按钮时,他可以转到下一个问题(而不会遗漏前一个文本)。
我正在尝试将元素设置为相对onclick.我的代码将div设置为relative,但是我需要将其他div重置为绝对定位.到目前为止我有......
<div class="case-studies">
<section class="pagination">
<div>one</div>
<div>two</div>
<div>three</div>
<div>four</div>
</section>
<div class="case-study" style="background:blue;"></div>
<div class="case-study" style="background:green;"></div>
<div class="case-study" style="background:orange;"></div>
<div class="case-study" style="background:pink;"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
$('.pagination div:first-of-type').on('click', function() {
$('.case-study:first-of-type').css("position", "relative");
});
$('.pagination div:nth-of-type(2)').on('click', function() {
$('.case-study:nth-of-type(2)').css("position", "relative");
});
$('.pagination div:nth-of-type(3)').on('click', function() {
$('.case-study:nth-of-type(3)').css("position", "relative");
});
$('.pagination div:last-of-type').on('click', function() {
$('.case-study:last-of-type').css("position", "relative");
});
Run Code Online (Sandbox Code Playgroud)