当用户悬停页面的某些元素时,我正在使用jQuery tip插件来显示帮助提示.
我需要在使用css选择器加载页面后注册插件事件.
问题是我使用的是ASP.NET更新面板,在第一次回发后,提示会停止工作,因为更新面板会替换页面内容,但不会重新绑定javascript事件.
在更新面板刷新其内容后,我需要一种方法来执行javascript回调,因此我可以重新绑定javascript事件以使提示再次起作用.
有没有办法做到这一点?
我想写一个类似于这个的方法:
+(void)myMethodWithView:(UIView *)exampleView completion:(void (^)(BOOL finished))completion;
Run Code Online (Sandbox Code Playgroud)
我基本上删除了Apple的一个类方法中的语法UIView:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion;
Run Code Online (Sandbox Code Playgroud)
并期望它像这样使用:
[myFoo myMethodWithView:self.view completion:^(BOOL finished){
NSLog(@"call back success");
}];
Run Code Online (Sandbox Code Playgroud)
我的问题是如何实现这一点?如果有人可以向我指出那些非常好的正确文档,并且非常感谢一个非常基本的例子(或者Stack Overflow上的类似答案 - 我找不到一个).我仍然不太了解代表,以确定这是否是正确的方法!
我已经在实现文件中给出了一个粗略的例子,但是由于我无法找到信息,所以猜测是有效的.
+ (void)myMethod:(UIView *)exampleView completion:(void (^)(BOOL finished))completion {
// do stuff
if (completion) {
// what sort of syntax goes here? If I've constructed this correctly!
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个具有:credits属性的User模型.我想要一个简单的按钮,通过一个名为"add"的路由将5添加到用户的信用点,这样/ users/3/add会将5添加到用户id = 3的信用点.
def add
@user = User.find(params[:id])
@user.credits += 5
redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)
这是我的控制器的相关部分.问题是,我不想调用@ user.save,因为我有一个before_save回调,它根据当前的UTC时间重新加密用户的密码.我只想简单地为属性添加5并避免回调,我从未想过这么简单的事情会如此困难.
编辑:
我将回调更改为:before_create,这是我的新控制器代码(相关部分):
def add
@user = User.find(params[:id])
@user.add_credits(5)
@user.save
flash[:success] = "Credits added!"
redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)
这是我在模型中的代码:
def add_credits(num)
self.credits = num
end
Run Code Online (Sandbox Code Playgroud)
编辑2:
好吧,这是一个验证问题,使"编辑"中的更改无法正常工作,但我仍然喜欢在没有回调的情况下更新原始问题的答案!
假设我有以下内容:
for(var i = 0; i < length; i++){
var variable = variables[i];
otherVariable.doSomething(variable, function(err){ //callback for when doSomething ends
do something else with variable;
}
Run Code Online (Sandbox Code Playgroud)
在调用回调时,variable将不可避免地成为所有回调的最后一个变量,而不是每个回调的不同变量,正如我所希望的那样.我意识到我可以传递variable给doSomething()然后作为回调的一部分传回,但是它doSomething()是外部库的一部分,我宁愿不要乱用它的源代码.
那些比我更了解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) 我正在使用一个API,要求我将函数指针作为回调传递.我正在尝试从我的类中使用此API,但是我遇到了编译错误.
这是我从构造函数中做的:
m_cRedundencyManager->Init(this->RedundencyManagerCallBack);
Run Code Online (Sandbox Code Playgroud)
这不编译 - 我收到以下错误:
错误8错误C3867:'CLoggersInfra :: RedundencyManagerCallBack':函数调用缺少参数列表; 使用'&CLoggersInfra :: RedundencyManagerCallBack'创建指向成员的指针
我尝试使用这个建议&CLoggersInfra::RedundencyManagerCallBack- 对我不起作用.
对此有何建议/解释?
我正在使用VS2008.
谢谢!!
我试图使用jQuery AJAX调用预加载一些图像,但是在将(url)字符串传递给AJAX调用的成功函数中的函数时遇到了实际问题(如果这是有道理的).
这是我的代码:
//preloader for images on gallery pages
window.onload = function() {
setTimeout(function() {
var urls = ["./img/party/"]; //just one to get started
for ( var i = 0; i < urls.length; i++ ) {
$.ajax({
url: urls[i],
success: function(data,url) {
$(data).find("a:contains(.jpg)").each(function(url) {
new Image().src = url + $(this).attr("href");
});
}
});
};
}, 1000);
};
Run Code Online (Sandbox Code Playgroud)
我可以看到我的(失败的)尝试将url传递到.each()调用中 - url最终获取增加整数的值.不知道为什么或这些与什么有关,也许是jpg文件的数量?
...无论如何,它当然应该采用我的原始urls数组中的单个值.
感谢您的帮助 - 我似乎总是对这些回调产生一些影响.
陆侃?
所以,我喋喋不休地听了@ron tornambe和@PiSquared的评论,目前我在这里:
//preloader for images on gallery pages
window.onload = function() { …Run Code Online (Sandbox Code Playgroud) 我来自JavaScript,其中回调非常简单.我试图将它们实现到JAVA中,但没有成功.
我有一个Parent类:
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
ExecutorService workers = Executors.newFixedThreadPool(10);
private ServerConnections serverConnectionHandler;
public Server(int _address) {
System.out.println("Starting Server...");
serverConnectionHandler = new ServerConnections(_address);
serverConnectionHandler.newConnection = function(Socket _socket) {
System.out.println("A function of my child class was called.");
};
workers.execute(serverConnectionHandler);
System.out.println("Do something else...");
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个从父级调用的子类:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ServerConnections implements Runnable {
private int serverPort;
private ServerSocket mainSocket;
public ServerConnections(int _serverPort) {
serverPort = _serverPort;
} …Run Code Online (Sandbox Code Playgroud) 我只发现了涉及类,事件处理程序和回调的相当复杂的答案(在我看来,这似乎是一个有点大锤的方法).我认为回调可能有用但我似乎无法在最简单的上下文中应用这些.看这个例子:
<html>
<head>
<script type="text/javascript">
function myfunction() {
longfunctionfirst();
shortfunctionsecond();
}
function longfunctionfirst() {
setTimeout('alert("first function finished");',3000);
}
function shortfunctionsecond() {
setTimeout('alert("second function finished");',200);
}
</script>
</head>
<body>
<a href="#" onclick="javascript:myfunction();return false;">Call my function</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在此,第二个函数在第一个函数之前完成; 什么是最简单的方法(或者有一个?)强制第二个函数延迟执行直到第一个函数完成?
- -编辑 - -
所以这是一个垃圾的例子,但多亏了David Hedlund,我看到这个新的例子确实它是同步的(同时在测试过程中崩溃我的浏览器!):
<html>
<head>
<script type="text/javascript">
function myfunction() {
longfunctionfirst();
shortfunctionsecond();
}
function longfunctionfirst() {
var j = 10000;
for (var i=0; i<j; i++) {
document.body.innerHTML += i;
}
alert("first function finished");
}
function shortfunctionsecond() {
var j …Run Code Online (Sandbox Code Playgroud) 我有这个柜台,但是我希望它能永远运行,这很简单,我在这里做错了什么?
function timer() {
console.log("timer!")
}
window.setInterval(timer(), 1000)
Run Code Online (Sandbox Code Playgroud) callback ×10
javascript ×5
jquery ×2
ajax ×1
asp.net-ajax ×1
asynchronous ×1
attributes ×1
c++ ×1
c++03 ×1
execution ×1
ios ×1
java ×1
model ×1
objective-c ×1
ruby ×1
setinterval ×1
synchronous ×1
updatepanel ×1