我试图让每个声明中的div淡入/淡出.问题是在淡入/淡出完成之前调用下一个项目.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"></script>
<div id='one'>one</div>
<div id='two'>two</div>
<div id='three'>three</div>
<script>
$.each([ "one", "two", "three"], function() {
console.log( 'start - ' + this );
animate( this );
console.log( 'end - ' + this );
});
function animate( id )
{
box = '#' + id;
$(box).fadeOut( 500, function( )
{
console.log('showing - ' + id);
$(box).fadeIn( 500 );
$(box).css('backgroundColor','white');
});
}
</script>
Run Code Online (Sandbox Code Playgroud)
控制台节目 -
start - one
end - one
start - two
end - two
start - three
end …Run Code Online (Sandbox Code Playgroud) 是否有一个同步等待函数不会占用.NET WPF中的UI线程?就像是:
Sub OnClick(sender As Object, e As MouseEventArgs) Handles button1.Click
Wait(2000)
'Ui still processes other events here
MessageBox.Show("Is has been 2 seconds since you clicked the button!")
End Sub
Run Code Online (Sandbox Code Playgroud) 任何人都可以在java中为我提供一个很好的小例子演示wait()和notify()功能.我试过下面的代码,但它没有显示我的预期.
public class WaitDemo {
int i = 10;
int display() {
System.out.println("Lexmark");
i++;
return i;
}
}
Run Code Online (Sandbox Code Playgroud)
public class ClassDemo1 extends Thread {
private WaitDemo wd = new WaitDemo();
public static void main(String[] args) {
ClassDemo1 cd1 = new ClassDemo1();
ClassDemo1 cd2 = new ClassDemo1();
cd1.setName("Europe");
cd2.setName("America");
cd1.start();
cd2.start();
}
synchronized void display() {
System.out.println("Hello");
notifyAll();
}
public void run() {
synchronized (this) {
try {
{
notify();
System.out.println("The thread is " + currentThread().getName());
wait();
System.out.println("The value is " …Run Code Online (Sandbox Code Playgroud) 这是OCaml中的简单游戏循环.显示状态,接收输入,并且状态提前.通过将线程延迟每个循环0.025秒,每秒帧数被限制为40.
main.ml:
let rec main (* state *) frame_time =
(* Display state here. *)
Input.get_input ();
(* Advance state by one frame here. *)
(* If less than 25ms have passed, delay until they have. *)
if((Sys.time ()) < (frame_time +. 0.025)) then
Thread.delay ((frame_time +. 0.025) -. (Sys.time ()));
main (* next_state *) (Sys.time ())
;;
let init =
Graphics.open_graph " 800x500";
let start_time = (Sys.time ()) in
main (* start_state *) start_time
;;
Run Code Online (Sandbox Code Playgroud)
对于此示例,该get_input函数只是将按键打印到窗口.
input.ml: …
我有一个想要监视另一个程序的Bourne shell(/ bin/sh)脚本(为了便携性).它应该启动另一个程序,然后等待它退出.当第二个程序退出时,它会做一些最后的工作并退出.问题在于脚本需要响应信号(例如USR2)并在这些信号出现时做一些工作.
我天真的实施是:
#! /bin/sh
echo $$
trap 'echo Respond to USR2' USR2
/bin/sleep 120 &
pid=$!
wait $pid
echo $pid exited with $?
echo Doing final cleanup
Run Code Online (Sandbox Code Playgroud)
这不起作用.如果我发送shell SIGUSR2,陷阱会按预期触发,但是等待也完成,返回140./bin/sleep继续它的快乐方式.典型输出:
28849
Respond to USR2
28850 exited with 140
Doing final cleanup
Run Code Online (Sandbox Code Playgroud)
这个行为在dash和bash之间是一致的,我可以方便地访问两个Bourne shell派生物.
我目前的工作是旋转循环等待子PID消失,用kill进行探测.自旋循环似乎很浪费,并且扩大了窗口,如果PID被快速重用,我的脚本可能会错误地等待错误的进程.
#! /bin/sh
echo $$
trap 'echo Respond to USR2' USR2
/bin/sleep 15 &
pid=$!
while /bin/kill -0 $pid 2> /dev/null; do
echo waiting...
sleep 2
done
echo Doing final cleanup
Run Code Online (Sandbox Code Playgroud)
鉴于我的目标是同时等待另一个进程退出并能够响应信号,是否有更好的解决方案?
在初始化表单(主窗体)时,它调用另一个窗体来获取一堆启动输入,然后传输大量信息:
Form3 getup = new Form3();
getup.Show();
example = getup.example;
Run Code Online (Sandbox Code Playgroud)
但是,我需要等待这个新的表单信息完成.
Form3 getup = new Form3();
getup.Show();
waitfordone();
example = getup.example;
Run Code Online (Sandbox Code Playgroud)
ATM,我尝试过使用while语句:
Form3 getup = new Form3();
getup.Show();
While(getup.visible=true)Console.WriteLine("waiting");
example = getup.example;
Run Code Online (Sandbox Code Playgroud)
但是这会导致挂起...也就是说,它会运行,然后冻结.我怀疑这是因为while循环正在吃掉所有的处理.所以,我试着创建一个新线程
Form3 getup = new Form3();
Thread t = new Thread(getup.Show());
t.start();
While(getup.visible=false)Console.WriteLine("waiting"); // takes a little bit to open
While(getup.visible=true)Console.WriteLine("waiting"); //waits for close
example = getup.example;
Run Code Online (Sandbox Code Playgroud)
但这也导致它挂起.也许出于同样的原因.我已经研究过autoresetevents.
我试过了:
AutoResetEvent invisible = new AutoResetEvent(false);
Form3 getup = new Form3();
void setup_invisible(object sender, EventArgs e)
{
if (getup.Visible == …Run Code Online (Sandbox Code Playgroud) 出于好奇,当Java实现wait()和notify()方法时,它们真的只是使用锁吗?即,wait()获取互斥锁,notify()释放互斥锁,notifyAll()释放所有互斥锁(当然在同一个对象中)?
除了比使用锁更简单之外,使用wait()和notify()还有其他优点吗?
[编辑]我意识到在Brian的评论之后我对此感到困惑:
等待没有锁定,它释放锁定并将其传递给正在等待互斥锁的同步语句的其他人,然后等待有锁定并通知的其他人通知,这会将锁定传回原始状态调用等待的线程.我认为这就是你感到困惑的地方. - 布赖恩17分钟前
我在java.lang.Object中使用wait()的定时版本,并观察到它在两种不同的场景中的行为不同.
场景1:在Thread中使用run()的默认定义
public static void main (String[] args) throws InterruptedException {
Thread t = new Thread();
t.start();
System.out.print("X");
synchronized(t) { t.wait(10000);}
System.out.print("Y");
}
Run Code Online (Sandbox Code Playgroud)
关于scenario1的问题:我遇到了X和Y之间的延迟.这是因为我从main调用wait()(即使在t上)因此正在使用主线程的调用堆栈,而不是第二个线程?
场景2:动态 子类化线程以覆盖run()以打印内容.
public static void main (String[] args) throws InterruptedException {
Thread t = new Thread() {public void run()
{System.out.print("I am the second thread.");}};
t.start();
System.out.print("X");
synchronized(t) { t.wait(10000);}
System.out.print("Y");
}
Run Code Online (Sandbox Code Playgroud)
关于场景2的问题:我没有遇到任何延迟!是什么改变只是因为我已经覆盖了run()?现在,每次我运行该程序时,它立即打印出"XI am the second thread.Y",无论如何都没有任何延迟!wait()的效果在哪里消失了?
在使用Python的selenium Webdriver中,我想等待Ajax请求完成(jquery库).我使用Selenium的wait.until()函数.单击submitJquery按钮后开始Ajax请求.
wait.until(self.driver.execute_script("return jQuery.active == 0"))
Run Code Online (Sandbox Code Playgroud)
但我得到以下错误:
E
======================================================================
ERROR: test_MahsumAkbasNet_Pass (__main__.TestClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\xxx\src\unittestpackage\JavaScriptExec.py", line 24, in test_MahsumAkbasNet_Pass
wait.until(self.driver.execute_script("return jQuery.active == 0"))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 66, in until
value = method(self._driver)
TypeError: 'bool' object is not callable
----------------------------------------------------------------------
Ran 1 test in 14.449s
FAILED (errors=1)
Run Code Online (Sandbox Code Playgroud)
完整的代码是:
# -*- coding: UTF-8 -*-
import unittest
import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as …Run Code Online (Sandbox Code Playgroud) 我正在编写一个计时器应用程序。在单元测试中,如何等待几秒钟以测试计时器是否正常工作?
// I want something like this.
test("Testing timer", () {
int startTime = timer.seconds;
timer.start();
// do something to wait for 2 seconds
expect(timer.seconds, startTime - 2);
});
Run Code Online (Sandbox Code Playgroud)