我知道这个问题已被无数次询问,但我无法弄清楚我的生活如何让这个答案在我的情况下工作:等待异步javascript函数返回
我正在循环通过外环中的一些"电视频道",然后在内环中循环一周中的日期.在内部循环中,我向服务器发出ajax请求以获取数据,然后我将其存储/缓存以供以后使用,如此
var dates = []; //<-- Contains a list of dates for the coming week
var baseUrl = "http://www.someserver.com";
var storedChannels = [1,2,3,4,5,6,7,8,9,10,45,23,56,34,23,67,23,567,234,67,345,465,67,34];
for(ch = 0; ch < storedChannels.length; ch++) {
var channel = storedChannels[ch];
for(d=0; d < 7; d++) {
var currentDate = dates[d];
ajax({
url: baseUrl+"?ch="+channel+"&dt=currentDate"+,
complete: function(res) {
CMLocalStore.setString('ch' + ch + "_" + scheduleDay, res);
},
});
//Want to wait here till the ajax request completes.
//Do not want to continue to next iteration. …Run Code Online (Sandbox Code Playgroud) 我有一个Java Thread来处理与a的传出通信Socket.我只希望线程在有待发送的待处理输出时运行.假设我有一个Stack<String>等待发送的数据,我希望当有些东西被添加到堆栈时唤醒通信线程并在堆栈为空时进入休眠状态.这是最好的方法吗?
我看到的选项是;
wait()/notify()- 现在似乎是实现这种行为的旧方法Thread每次构建一个新的(昂贵的)任何建议都会很棒:)
我正在使用JProfiler来分析我的应用程序,因此,在"CPU视图"部分中,它表明花费了超过40%的CPU时间Object.wait().但据我所知,在Object.wait()CPU上没有给予等待线程.
有人可以帮助了解发生了什么以及为什么分析器显示这么多的CPU花费了Object.wait()吗?
有没有办法让a webDriverWait等待一些元素出现,并根据出现的元素采取相应的行动?
目前我WebDriverWait在try循环中执行一个操作,如果发生超时异常,我会运行替代代码,等待另一个元素出现.这看起来很笨拙.有没有更好的办法?这是我(笨拙)的代码:
try:
self.waitForElement("//a[contains(text(), '%s')]" % mime)
do stuff ....
except TimeoutException:
self.waitForElement("//li[contains(text(), 'That file already exists')]")
do other stuff ...
Run Code Online (Sandbox Code Playgroud)
它需要等待整整10秒才能查看该文件是否已存在于系统上的消息.
该函数waitForElement只执行了许多WebDriverWait调用:
def waitForElement(self, xPathLocator, untilElementAppears=True):
self.log.debug("Waiting for element located by:\n%s\nwhen untilElementAppears is set to %s" % (xPathLocator,untilElementAppears))
if untilElementAppears:
if xPathLocator.startswith("//title"):
WebDriverWait(self.driver, 10).until(lambda driver : self.driver.find_element_by_xpath(xPathLocator))
else:
WebDriverWait(self.driver, 10).until(lambda driver : self.driver.find_element_by_xpath(xPathLocator).is_displayed())
else:
WebDriverWait(self.driver, 10).until(lambda driver : len(self.driver.find_elements_by_xpath(xPathLocator))==0)
Run Code Online (Sandbox Code Playgroud)
有人建议以更有效的方式完成这项工作吗?
我收到警告>隐式声明函数'wait'<当我运行程序它正常工作时,我想明白为什么我收到这个警告?
提前致谢
编辑:我忘了添加包含的库
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void create (char* program, char** arg_list)
{
/* put your code here */
pid_t childPid;
int status;
if((childPid = fork()) < 0){
printf("Failed to fork() --- exiting...\n");
exit(1);
}
else if (childPid == 0){ // --- inside the child process
if(execvp(program, arg_list) < 0){ // Failed to run the command
printf("*** Failed to exec %s\n", program);
exit(1);
}
}
else{ // --- parent process
while(wait(&status) != childPid)
printf("...\n");
} …Run Code Online (Sandbox Code Playgroud) 这是线程正在等待notify()或超时的情况.这里添加了一个while循环来处理虚假唤醒.
boolean dosleep = true;
while (dosleep){
try {
wait(2000);
/**
* Write some code here so that
* if it is spurious wakeup, go back and sleep.
* or if it is timeout, get out of the loop.
*/
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我如何区分虚假唤醒和超时?如果它是一个虚假的醒来,我需要回去等待.如果是超时,我需要退出循环.
我可以很容易地识别notify()的情况,因为我将在notify()调用时将dosleep变量设置为false.
编辑:由于嵌入式项目的要求,我使用的是1.4版java.我不能使用,Condition因为它只在1.5后可用.
提前致谢.
我是C#的新手并且正在使用Task.我试图运行这个应用程序但我的应用程序每次都挂起.当我添加时task.wait(),它会一直等待,永远不会返回.任何帮助深表感谢.编辑:我想异步调用DownloadString.当我按照"Austin Salonen"的建议执行task.Start()时,我没有得到位置的地址,而是从returnVal获取位置字符串中的默认值.这意味着在任务完成之前,位置得到了值.如何确保在任务完成后只有位置被指定returnVal.
public class ReverseGeoCoding
{
static string baseUri = "http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false";
string location = "default";
static string returnVal = "defaultRet";
string latitude = "51.962146";
string longitude = "7.602304";
public string getLocation()
{
Task task = new Task(() => RetrieveFormatedAddress(latitude, longitude));
//var result = Task.Factory.StartNew(RetrieveFormatedAddress("51.962146", "7.602304"));
task.Wait();
//RetrieveFormatedAddress("51.962146", "7.602304");
location = returnVal;
return location;
}
public static void RetrieveFormatedAddress(string lat, string lng)
{
string requestUri = string.Format(baseUri, lat, lng);
using (WebClient wc = new WebClient())
{
wc.DownloadStringCompleted …Run Code Online (Sandbox Code Playgroud) 基本上我试图使用JavaFX做一个简短的效果.我有一个心脏的形状(从两个圆圈和一个多边形加起来),我可以使用double值改变大小p."标准尺寸"将是p = 1.0;.
我试图给心脏增加一个泵效应.我有方法pumpOnce():
public void pumpOnce(){
p = p + 1;
initHeart();
//Here goes what ever it takes to make stuff working!!
p = p - 1;
initHeart();
}
Run Code Online (Sandbox Code Playgroud)
initHeart()吸取内心的基础p.
我发现Thread.sleep();由于JavaFX中的线程原理,或类似的方法不起作用.
但是我可以用什么呢?
wait ×10
java ×4
ant ×1
build ×1
c ×1
c# ×1
cpu ×1
google-api ×1
javafx ×1
javascript ×1
performance ×1
pid ×1
profiling ×1
puppet ×1
python ×1
selenium ×1
synchronous ×1
task ×1
thread-sleep ×1
webdriver ×1