- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 ajax 请求成功后仅下一次迭代。
您可以在开发控制台上清楚地看到这里发生了什么。 foreach
循环中有一个 console.log(...);
函数。而这个条件应该永远为真。我可以在这两个 AJAX 请求中使用 async: false
实现此结果,但是,它会阻止 UI。
在我的特殊情况下,它不是一个普通的网页,只是一个项目,我可以这样做,但是,我不想使用它。我需要其他想法。
Console.log(....);
函数在不同时间返回不同的值,但它应该始终在左侧和右侧返回相同的值。
例如:ESL_SC2 == habathcx
- 错误,ESL_SC2 == ESL_SC2
- 正确。
打开控制台查看结果。只看JS部分。
$(function() {
$("html").removeClass("no-js");
$("#tabs").tabs();
var xhr = new window.XMLHttpRequest(),
users = [
"ESL_SC2",
"OgamingSC2",
"cretetion",
"freecodecamp",
"storbeck",
"habathcx",
"RobotCaleb",
"noobs2ninjas",
"mhayia"
],
index,
ajaxDone,
userResults = [],
$search = $("#search"),
keyCode,
request;
var getUserResults = function(callback) {
index = 0;
users.forEach(function(e) {
$.ajax({
url: "https://wind-bow.glitch.me/twitch-api/users/" + e,
beforeSend: function() {
ajaxDone = false;
},
success: function(d) {
if (d.display_name !== undefined) {
userResults[index] = {
name: d.display_name
};
console.log(users[index] + " == " + e);
}
index++;
if (index == users.length) {
callback();
}
ajaxDone = true;
},
error: function() {
alert(
"AJAX Request failed. Please try again or contact using email n3olukas@gmail.com."
);
},
datatype: "json",
cache: false
});
});
};
getUserResults(function() {
index = 0;
users.forEach(function(e) {
$.ajax({
url: "https://wind-bow.glitch.me/twitch-api/streams/" + e,
success: function(d) {
if (d.stream !== null) {
userResults[index].status = d.stream.channel !== undefined ? 'Online' : '';
userResults[index].title = d.stream.channel !== undefined ? d.stream.channel.status != undefined ? d.stream.channel.status : '' : '';
userResults[index].url = d.stream.channel !== undefined ? d.stream.channel.url !== undefined ? d.stream.channel.url : '' : '';
}
if (userResults[index].name) {
$('#tabs-1 table').append('<tr><td>' + userResults[index].name + '</td><td>' + (userResults[index].status ?
'Online' : 'Offline') + '</td><td>' + (userResults[index].title ?
userResults[index].title : '') + '</td></tr>');
}
index++;
$("body").addClass("loaded");
},
error: function() {
alert(
"AJAX Request failed. Please try again or contact using email n3olukas@gmail.com."
);
},
datatype: "json",
cache: false
});
});
});
});
body {
margin: 0;
padding: 0;
font-family: sans-serif;
background-image: url("https://images8.alphacoders.com/702/702959.jpg");
background-attachment: fixed;
background-size: cover;
}
* {
box-sizing: border-box;
}
.text-center {
text-align: center;
}
.input {
overflow: hidden;
white-space: nowrap;
}
#tabs ul {
margin: 0;
padding: 0;
list-style: none;
}
#tabs a {
background-color: #f44336;
width: 33.33%;
width: calc(100% / 3);
padding: 10px 0;
float: left;
}
#search {
width: 100%;
height: 3.125rem;
border: none;
font-size: 13px;
color: #4f5b66;
padding: 0 .9375rem;
}
.three-dots {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
table {
width: 100%;
border-spacing: 0;
border-collapse: collapse;
table-layout: fixed;
}
table tr {
background-color: #f44336;
color: #fff;
}
tr:first-child {
background-color: #673ab7;
}
table td {
padding: 0.625rem 0;
}
:focus {
outline: 0;
}
.ui-state-active a {
background-color: #2196f3 !important;
}
footer {
font-size: 0.85rem;
margin: 1rem 0;
}
a {
text-decoration: none;
color: #fff;
position: relative;
}
footer a:before {
content: "";
position: absolute;
width: 100%;
height: 0.0625rem;
bottom: 0;
left: 0;
background-color: #fff;
visibility: hidden;
-webkit-transform: scaleX(0);
transform: scaleX(0);
-webkit-transition: all 0.3s ease-in-out 0s;
transition: all 0.3s ease-in-out 0s;
}
footer a:hover:before {
visibility: visible;
-webkit-transform: scaleX(1);
transform: scaleX(1);
}
.search-results {
background: #fff;
margin: 1.5625rem 0;
border-left: 0.3125rem solid #0ebeff;
opacity: 0;
display: none;
}
.search-results h4,
.search-results p {
margin: 0;
padding: 0.625rem;
text-align: left;
}
.search-results a {
color: #0ebeff;
display: inline-block;
margin: 1rem 0;
}
.search-results a:before {
background-color: #0ebeff;
}
.wikisearch-container {
width: 65%;
margin: 2.5rem auto 0;
}
/* Screen loader */
#loader-wrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1000;
}
#loader {
display: block;
position: relative;
left: 50%;
top: 50%;
width: 9.375rem;
height: 9.375rem;
margin: -4.6875rem 0 0 -4.6875rem;
border-radius: 50%;
border: 0.1875rem solid transparent;
border-top-color: #fff;
-webkit-animation: spin 1.75s linear infinite;
/* Chrome, Opera 15+ Safari 5+ */
animation: spin 1.75s linear infinite;
/* Chrome, Firefox 16+, IE 10+, Opera */
z-index: 1001;
}
#loader:before {
content: "";
position: absolute;
top: 0.3125rem;
left: 0.3125rem;
right: 0.3125rem;
bottom: 0.3125rem;
border-radius: 50%;
border: 0.1875rem solid transparent;
border-top-color: #f7d130;
-webkit-animation: spin 1.5s linear infinite;
/* Chrome, Opera 15+ Safari 5+ */
animation: spin 1.5s linear infinite;
/* Chrome, Firefox 16+, IE 10+, Opera */
}
#loader:after {
content: "";
position: absolute;
top: 0.9375rem;
left: 0.9375rem;
right: 0.9375rem;
bottom: 0.9375rem;
border-radius: 50%;
border: 0.1875rem solid transparent;
border-top-color: #0fff;
-webkit-animation: spin 1.25s linear infinite;
/* Chrome, Opera 15+ Safari 5+ */
animation: spin 1.25s linear infinite;
/* Chrome, Firefox 16+, IE 10+, Opera */
}
@-webkit-keyframes spin {
0% {
-webkit-transform: rotate(0deg);
/* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(0deg);
/* IE 9 */
transform: rotate(0deg);
/* Firefox 16+, IE 10+, Opera */
}
100% {
-webkit-transform: rotate(360deg);
/* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(360deg);
/* IE 9 */
transform: rotate(360deg);
/* Firefox 16+, IE 10+, Opera */
}
}
@keyframes spin {
0% {
-webkit-transform: rotate(0deg);
/* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(0deg);
/* IE 9 */
transform: rotate(0deg);
/* Firefox 16+, IE 10+, Opera */
}
100% {
-webkit-transform: rotate(360deg);
/* Chrome, Opera 15+, Safari 3.1+ */
-ms-transform: rotate(360deg);
/* IE 9 */
transform: rotate(360deg);
/* Firefox 16+, IE 10+, Opera */
}
}
.loaded #loader-wrapper,
.loader-section {
position: fixed;
top: 0;
width: 50%;
height: 100%;
background: #000428;
z-index: 1000;
}
#loader-wrapper .loader-section.section-left {
left: 0;
}
#loader-wrapper .loader-section.section-right {
right: 0;
}
.loaded #loader-wrapper .loader-section.section-left {
-webkit-transform: translateX(-100%);
/* Chrome, Opera 15+, Sadari 3.1+ */
-ms-transform: transalteX(-100%);
/* IE 9 */
transform: translateX(-100%);
/* Firefox 16+ IE 10+, Opera */
-webkit-transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
/* Android 2.1+ Chrome 1-25, iOS 3.2-6.1, Safari 3.2-6 */
transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
/* Chrome 26, Firefox 16+, iOS 7+, IE 10+, Opera, Safari 6.1+ */
}
.loaded #loader-wrapper .loader-section.section-right {
-webkit-transform: translateX(100%);
/* Chrome, Opera 15+, Sadari 3.1+ */
-ms-transform: transalteX(100%);
/* IE 9 */
transform: translateX(100%);
/* Firefox 16+ IE 10+, Opera */
-webkit-transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
/* Android 2.1+ Chrome 1-25, iOS 3.2-6.1, Safari 3.2-6 */
transition: all 0.7s 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
/* Chrome 26, Firefox 16+, iOS 7+, IE 10+, Opera, Safari 6.1+ */
}
.loaded #loader {
opacity: 0;
position: fixed;
-webkit-transition: all 0.3s ease-out;
/* Android 2.1+ Chrome 1-25, iOS 3.2-6.1, Safari 3.2-6 */
transition: all 0.3s ease-out;
/* Chrome 26, Firefox 16+, iOS 7+, IE 10+, Opera, Safari 6.1+ */
}
.loaded #loader-wrapper {
visibility: hidden;
-webkit-transition: all 0.3s ease-out;
/* Android 2.1+ Chrome 1-25, iOS 3.2-6.1, Safari 3.2-6 */
transition: all 0.3s ease-out;
/* Chrome 26, Firefox 16+, iOS 7+, IE 10+, Opera, Safari 6.1+ */
}
.no-js #loader-wrapper {
display: none;
}
/* Loading animation */
/* Loading animation */
@keyframes lds-eclipse {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
50% {
-webkit-transform: rotate(180deg);
transform: rotate(180deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@-webkit-keyframes lds-eclipse {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
50% {
-webkit-transform: rotate(180deg);
transform: rotate(180deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
.loading {
position: relative;
top: 0.59375rem;
right: 0.9375rem;
pointer-events: none;
display: none;
}
.lds-eclipse {
-webkit-animation: lds-eclipse 1s linear infinite;
animation: lds-eclipse 1s linear infinite;
width: 2rem;
height: 2rem;
border-radius: 50%;
margin-left: auto;
box-shadow: 0.08rem 0 0 #0ebeff;
}
@media (max-width: 71.875em) {
.wikisearch-container {
width: 75%;
}
}
@media (max-width: 50em) {
.wikisearch-container {
width: 85%;
}
}
@media (max-width: 17.96875em) {
.wikisearch-container {
width: 100%;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div id="loader-wrapper">
<div id="loader">
</div>
<div class="loader-section section-left"></div>
<div class="loader-section section-right"></div>
</div>
<div class="twitchtv-container text-center">
<div id="tabs">
<ul>
<li><a href="#tabs-1">All</a></li>
<li><a href="#tabs-2">Online</a></li>
<li><a href="#tabs-3">Offline</a></li>
</ul>
<input type="text" id="search" placeholder="Search...">
<div id="tabs-1">
<table>
<tr>
<td>User</td>
<td>Status</td>
<td>Title</td>
</tr>
</table>
</div>
<div id="tabs-2">
<table>
<tr>
<td>User</td>
<td>Status</td>
<td>Title</td>
</tr>
</table>
</div>
<div id="tabs-3">
<table>
<tr>
<td>User</td>
<td>Status</td>
</tr>
</table>
</div>
</div>
<footer>
<a href="https://codepen.io/Kestis500">Created by LukasLSC</a>
</footer>
</div>
最佳答案
虽然有“异步迭代”技术,但一个接一个地获取一个用户是发出多个单独请求的缓慢方法。对于这样的情况,并行进行提取要快得多。这就是您当前正在做的事情,但正如您已经意识到的那样,挑战是以与请求相同的顺序返回响应。这是一个 promises 很好解决的问题(使用 Promise.all
和 fetch
)。由于您已经在使用 jQuery,因此您也可以使用 jQuery 的 Deferred objects 来完成它和 $.when() :
var users = [
"ESL_SC2",
"OgamingSC2",
"cretetion",
"freecodecamp",
"storbeck",
"habathcx",
"RobotCaleb",
"noobs2ninjas",
"mhayia"
];
// Transform array of usernames into an array of jQuery deferreds for
// each request. Note that this is actually sending all the requests
// in parallel.
var requests = users.map(function(user) {
// No need to handle success/failures here, we'll chain those handlers
// onto the `$.when()` call below.
return $.ajax({
url: "https://wind-bow.glitch.me/twitch-api/users/" + user,
datatype: "json",
cache: false
});
});
// Use jQuery.when() to wait for all requests to complete, and provide
// the responses in order (this is the purpose of $.when() or Promise.all()).
// Note that $.when() expects multiple arguments so we need accomodate that
// by spreading our array with Function.prototype.apply(). (This could be
// written more simply in ES2015 as $.when(...requests)).
$.when.apply(null, requests)
// done() is called after all requests are complete. All responses are
// passed as arguments in the order the requests were sent.
.done(function() {
// Loop through all arguments. Using a `for` loop because built-in
// `arguments` object isn't a real array, so we can't use
// arguments.forEach().
for (var i=0; i<arguments.length; i++) {
var response = arguments[i];
// Response is an array, the first element is the user object we want.
var user = response[0];
console.log(users[i] + " == " + user.display_name);
}
})
// If any request fails, it will be handled here.
.fail(function() {
alert('User request failed.');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
为了举例,这里有两个使用 Chrome 的开发工具记录的网络瀑布,模拟了“快速 3G”速度。并行版本快了 5 倍。
关于javascript - ajax请求成功后才循环遍历foreach循环数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48602440/
我正在使用 jQuery 的 $.ajax 函数来提交表单,它可以工作,但成功正是我遇到问题的地方。这是我的代码: $("#form").submit(function () { $.
我正在使用动态分页。 我需要在开始另一个事件之前取消 jQuery ajax 中的 success 事件。 我已经设置了一个等于$.ajax()的变量,在这样做之前,无论如何我都会调用abort。 问
如果我错了,请纠正我,但我对 $.post 成功/失败的理解是,如果 url 有效,这将返回成功。唯一会返回失败的情况是 url 无效。 如果这是真的,我如何验证成功函数?我问的原因是无论发生什么,即
HANDLE hFile = CreateFile(LPCTSTR("filename"), // name of the write
我正在使用以下代码发送短信。但这似乎不会在未发送短信时产生异常。例如,当没有足够的钱发送时,我仍然会去 smsSucces();有没有人知道解决此问题的方法以确保它已发送? private b
我正在尝试将字符串转换为 DateTime,在一台计算机上,它工作正常,但在另一台计算机上,它却不行!它运行的计算机运行的是 32 位 Windows 7,它不运行的计算机运行的是 64 位 Wind
我在页面上使用表单让用户输入将用于各种目的的图像的 url。我正在编写一个 ajax 方法来确定他们提供的 url 是否实际上是图像。到目前为止,我已经这样做了: $(document).on('re
我在 jquery 中对 php 脚本进行 ajax 调用。但是 php 脚本需要返回什么才能触发 ajax 中的成功/错误处理程序。所以这是 ajax: $.ajax({ data:
几个简单的问题: 对于 native 和 Flash/Silverlight 垫片来说,成功事件是“规范化”事件吗?记录的示例表明它仅适用于 Flash/Silverlight 对象准备就绪的情况。
这个问题不太可能对任何 future 的访客有帮助;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于互联网的全局受众。如需帮助使这个问题更广泛适用,visit the h
我尝试使用新的 Groovy Grape Groovy 1.6-beta-2 中的功能,但我收到一条错误消息; unable to resolve class com.jidesoft.swing.J
我正在使用 sequelize/nodejs/express/react 将实体持久化到 postgres 数据库 我有两个主要模型,国家和事件,我正在使用该应用程序,并且有一个名为“保存到数据库”的
我有以下代码,其中有 2 个电子邮件输入字段,我需要验证它们是否相同,并且使用 jQuery validate equalTo 成功运行。 Email Address
我正在尝试找出解决此问题的正确方法。 假设我们有一家元素商店。这些项目可以编辑、删除和创建。编辑或添加项目时,路线更改为/item/add 或/item/edit/{id}。 在 saga 成功添加或
这个问题已经有答案了: How do I return the response from an asynchronous call? (42 个回答) 已关闭 8 年前。 我有这段代码,警报工作正常
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
我想在单击超链接 (.remove_resort) 时(成功的 ajax 调用后)删除超链接的(父)跨度。 虽然ajax调用成功,但是最后span并没有被移除。这里出了什么问题? 请记住:有几个类
我正在编写一个非常简单的程序来将鼠标剪辑到指定的窗口。它从系统托盘运行,没有可见窗口。由于同一窗口会有多个实例,因此它使用 EnumWindows() 迭代每个顶级窗口,并将它们的 hwnd 与 Ge
我正在尝试找出如何执行 if 语句,以便如果玩家的击球率超过 0.250,则会为成功的 tr 添加一个类别。 我发现了以下堆栈问题,但我不确定可以使用或应该使用哪种方式以及如何使用这些堆栈问题。 ht
我是 Prolog 的新手,我正在尝试解决这个练习: Define a predicate greater_than/2 that takes two numerals in the notation
我是一名优秀的程序员,十分优秀!