我在循环中使用jQuery"GET"从服务器获取几个结果.我想将循环索引作为固定参数包含在回调中,但它不起作用.
(我遵循了本文关于如何做的建议.)
但是,我在回调中得到的值完全不是我所期望的 - 而不是每个循环索引值,它总是等于索引的退出值.
即.这里的代码片段为每次回调执行打印出'16'.如何让它打印1,2,3 ...(我意识到订单可能不同,那很好)
除了下面的代码,我还尝试了几种方法来指定回调函数,例如.function(data, textStatus) { return test(data, textStatus, idx); }, 'text');等等
这应该怎么样?
function test(data, textStatus, siteNo)
{
console.log("siteNo=" + siteNo);
}
function loadConfigLists()
{
var siteReport;
// retrieve site configuration
jQuery.get("svGetSiteConfig.php", function(data, textStatus)
{
// retrieve port configuration for all sites
for (var idx=1; idx<=15; idx++)
{
var probeIP = siteConfigArray[idx].siteIP;
if (probeIP != "" && probeIP != null)
jQuery.get("svGetPortInfo.php?svSiteIpAddr=" + probeIP+"&s="+idx,
function(data, textStatus) { test(data, textStatus, idx); }, 'text');
else …Run Code Online (Sandbox Code Playgroud) 我通过各种线程像阅读这样一个的例子.
但它真的让我无法完成以下任务:
我有4个功能,希望它们按顺序依次发生.请注意它们的顺序不正确,以便了解我的观点.我想要输出"1,2,3,4"的结果
function firstFunction(){
// some very time consuming asynchronous code...
console.log('1');
}
function thirdFunction(){
// definitely dont wanna do this until secondFunction is finished
console.log('3');
}
function secondFunction(){
// waits for firstFunction to be completed
console.log('2');
}
function fourthFunction(){
// last function, not executed until the other 3 are done.
console.log('4');
}
Run Code Online (Sandbox Code Playgroud)
我试图找出回调,但我迷路了:(
有没有一些简单的方法来做到这一点?就像在数组中循环一样......
我正在处理的单页面应用程序有一个登录视图,有两种形式:登录表单和注册表单.以下规范描述了这些表单的测试.我正在使用Jasmine-jQuery 1.4.2.
// user_spec.js
describe("User", function() {
var userController;
beforeEach(function () {
loadFixtures('menu.html');
userController = new MyApp.User.Controller();
});
describe("LoginView", function() {
beforeEach(function() {
// Mock the $.ajax function to prevent XHR:
spyOn($, "ajax").andCallFake(function(params) {});
});
it("should pass email and password with the 'signInForm:submit' event.", function() {
var email = "firstname.name@email.com";
var password = "Passw0rd";
var callback = jasmine.createSpy("FormSubmitSpy");
userController.loginView.$el.find("#signInEmail").val(email);
userController.loginView.$el.find("#signInPassword").val(password);
userController.loginView.bind("signInForm:submit", callback, this);
userController.loginView.ui.signInForm.trigger("submit");
expect(callback).toHaveBeenCalledWith({
email: email,
password: password
});
});
it("should pass name, email and password with the …Run Code Online (Sandbox Code Playgroud) javascript jquery-callback jasmine jasmine-jquery single-page-application
我在代码库中找到$(document).ready(function() {...}了另一个代码$(document).ready(function() {...}
例如
$(document).ready(function() {
// 20 lines...
$(document).ready(function() {
foo()
}
// 200 lines...
}
function foo() {...}
Run Code Online (Sandbox Code Playgroud)
我想了解执行的顺序,所以我可以安全地重构这个嵌套的回调.外部回调似乎在内部回调执行之前继续执行.外部回调是否保证在内部回调被调用之前完成?
我在jQuery ajax中遇到一个简单的回调函数问题.谷歌不会帮助,堆栈溢出也不会,所以我想这可能不是特定的东西,而是我无知的东西.对我来说代码看起来应该是这样的.
那么,这是代码:
function sendMessage(message)
{
//Establish connection to php script
$.ajax({
type: 'POST',
url: 'action/chat/test.php',
success: function(feedback){
alert(feedback);
}
}).error(function(){
//Do some error handling here
});
}
Run Code Online (Sandbox Code Playgroud)
在test.php中它简单地说
<?php
echo "called";
?>
Run Code Online (Sandbox Code Playgroud)
据我所知,"被叫"应该被警告 - 但事实并非如此.我已经检查过调用函数sendMessage()(现在参数消息无关紧要).
有谁有想法吗?
我正在编写一个jQuery插件,它"指向"DOM中的一定数量的节点.然而,如果我尝试让我的插件保持对一组节点的引用,我担心它们会变得"陈旧".
(虽然我意识到JavaScript是垃圾收集并且不会崩溃,但我希望能够使我的列表保持最新状态,而不是坚持应该是GC的东西.)
我遇到的第一件事是可能会有某种钩子.但似乎没有一个看起来值得信赖的标准:
使用JQuery,当DOM元素调用.remove()时,是否可以运行函数?
这让我想通过在它们上面放置一个类属性来维护节点列表.这样,他们在列表中的成员资格就会与他们一起旅行.但是,正如人们可能会担心的那样,这在枚举时可能在病理上很慢,这就是为什么你应该在课前将你的查询表示为标签.
除了潜在的性能问题之外,我想知道插件是否因为这种目的将类戳到DOM节点上而被认为是糟糕的形式,这与样式无关.(其中一个更好的事情.data()是,它是相对带外的,除了这个列表问题,我正在使用它.)
这似乎是其他插件已经解决的常见问题.我很想使用类解决方案来获得它的"正确性"属性,即使速度较慢.但是,是否有一种更快,更规范的方式可以提供两全其美的效果?
我有一个试图从浏览器获取位置设置的应用程序,这往往需要一点时间,所以我希望它在页面加载时运行.但是,如果在运行位置回调之前单击"提交"按钮,则表示没有位置数据.我的问题很简单,如何在提交表单之前等待我的位置成功回调完成?(没有像睡眠声明那样愚蠢的东西).
理想情况下,我想要一个忙碌的指示灯并等待数据.这可能吗?我有一个代码可以使繁忙的指示器可见,但我不确定如何优雅地等待数据可用.
$(document).ready(function(){
var lat = "";
var lng = "";
var accuracy = "";
var locationFound = false;
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function positionSuccess(loc){
lat = loc.coords.latitude;
lng = loc.coords.longitude;
accuracy = loc.coords.accuracy;
locationFound = true;
});
}else{
alert("I'm sorry, your jerk browser doesn't support geolocation");
locationFound = true;
}
$('#locForm').submit(function(e){
$('#lat').val(lat);
$('#lng').val(lng);
$('#boundary').val($('#boundary-select').val());
}
Run Code Online (Sandbox Code Playgroud) change()事件完成后如何调用一次函数?
例如,像这样:(我知道jQuery默认没有回调方法)
$('#element').change( function(){
// do something on change
// $('#milestonesSelect').multiselect({ minWidth: 120 , height : '200px' ,selectedList: 4 }).multiselectfilter();
// some animation calls ...
// ...
}, function(){
// do something after complete
alert('another codes has completed when i called');
}
);
Run Code Online (Sandbox Code Playgroud)
完成更改方法的所有代码后,是否可以调用单个回调,除了为其上的每个函数调用完整的回调?
我需要在更改事件完成后执行某些操作
我需要在变更处理程序中为方法设置顺序吗?
我创建了一个名为SearchBox的类来处理搜索交互(延迟触发,搜索输入键按下,防止搜索活动时,搜索完成时同步结果,文本更改等).
所有类方法都是原型方法,意味着可以通过访问this.在下面的代码中,假设p是类的原型.
p.registerListeners = function () {
$(this.element).on('keypress', this.searchKeyPressed);
};
p.unregisterListeners = function () {
$(this.element).off('keypress', this.searchKeyPressed);
};
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为当keypress事件调用searchKeyPressed处理程序时,它不会在上下文中这样做this.我能想到的唯一解决方案是只有现代浏览器支持的解决方案,即绑定回调this,实际创建一个新函数.由于它创建了一个新函数,我必须将其缓存以便以后删除它,因为我必须将相同的引用off传递给我传递给它的函数on.
有没有比这更好的方法,或者这样可以吗?
var boundKeyPressed;
p.registerListeners = function () {
boundKeyPressed = this.searchKeyPressed.bind(this);
$(this.element).on('keypress', boundKeyPressed);
};
p.unregisterListeners = function () {
$(this.element).off('keypress', boundKeyPressed);
};
Run Code Online (Sandbox Code Playgroud)
我认为这可能jQuery.on会提供一种自动执行此事件绑定的方法,但它似乎会this根据它的调用方式绑定到不同的事物.例如,在使用时on('eventname',instance.func),this是"currentTarget"(在冒泡术语中不一定是"目标"),而在使用时on('eventname','selector',instance.func),this是指与选择器匹配的元素.在任何一种情况下,func运行就好像它没有关系instance.
这是我的代码:
page.onResourceRequested = function (requestData, networkRequest) {
console.log(requestData.url); // This message shows http://website-with-jquery-included.com/page.html!
}
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.open('http://website-with-jquery-included.com/', function(status) {
if (status === "success") {
page.evaluate(function() {
console.log('I can see this message');
$.ajax({url: 'http://website-with-jquery-included.com/page.html', success: function(resp) {
console.log('I cannot see this message!');
}});
});
}
});
Run Code Online (Sandbox Code Playgroud)
函数page.onResourceError打印错误消息:
无法加载资源(#35URL:http://website-with-jquery-included.com/page.html )
错误代码:5.说明:操作已取消 …
jquery-callback ×10
javascript ×7
jquery ×7
ajax ×2
callback ×2
asynchronous ×1
closures ×1
geolocation ×1
jasmine ×1
phantomjs ×1
sequential ×1