我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo?
我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.
function foo() {
var result;
$.ajax({
url: '...',
success: function(response) {
result = response;
// return response; // <- I tried that one as well
}
});
return result;
}
var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud) 如果您希望事件在您的页面上运行,您应该在$(document).ready()函数内调用它.在加载DOM之后和加载 页面内容之前,其中的所有内容都将加载.
我想在加载页面内容后才能执行javascript代码我该怎么办?
我遇到了普通(非ajax)函数的问题,这些函数在每个函数中都涉及很多动画.目前我只有一个setTimeout功能之间,但这并不完美,因为没有浏览器/计算机是相同的.
附加说明:它们都有单独的动画/等碰撞.
我不能简单地把一个放在另一个的回调函数中
// multiple dom animations / etc
FunctionOne();
// What I -was- doing to wait till running the next function filled
// with animations, etc
setTimeout(function () {
FunctionTwo(); // other dom animations (some triggering on previous ones)
}, 1000);
Run Code Online (Sandbox Code Playgroud)
无论如何在js/jQuery中有:
// Pseudo-code
-do FunctionOne()
-when finished :: run -> FunctionTwo()
Run Code Online (Sandbox Code Playgroud)
我知道$.when()&$.done(),但那些是针对AJAX的......
jQuery有一个名为$ .timers的暴露变量(由于某种原因未在jQuery文档中列出),它包含当前正在进行的动画数组.
function animationsTest (callback) {
// Test if ANY/ALL page animations are currently active …Run Code Online (Sandbox Code Playgroud)