我有一个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) 我需要在PhantomJS中等一段时间.我搜索了很多,但没有找到答案.我在里面尝试这个代码,page.open但它不起作用.
var interval = window.setInterval(function(){
console.log("works");
clearInterval(interval);
}, 3000);
Run Code Online (Sandbox Code Playgroud)
我也试过,setTimeout但也没有帮助
window.setTimeout(function(){
console.log("works");
}, 3000);
Run Code Online (Sandbox Code Playgroud)
什么是等待几秒钟的解决方案?
我需要在日志之间等待3秒:jquery included和works.但这些日志同时出现在控制台中.
var page = require("webpage").create();
var url = "https://www.youtube.com";
page.open(url, function(status) {
if (status == "success"){
console.log(status);
if (page.injectJs("jquery.min.js")){
console.log("jquery included");
page.evaluate(function(){
setTimeout(function(){
}, 3000);
});
console.log("works");
phantom.exit();
}
}
else{
console.log(status);
phantom.exit();
}
});
Run Code Online (Sandbox Code Playgroud)