如何在 Javascript 中锁定线程?
我想在一个线程运行时锁定其他线程。在.Net 中可以使用锁对象。我需要在 AJAX 中做同样的事情。
function f_OrnekleriGetir(bKaydet) {
var ddlKlinikId = "<%=ddlKliniklker.ClientID%>";
var iKlinikId = $("#" + ddlKlinikId + " option:selected").val();
var arrOrnekNolar = new Array();
$(".tdOrnekNo").each(function(idx, td) {
var sOrnekNo = $(td).html();
arrOrnekNolar[idx] = sOrnekNo;
});
for (var i = 0; i < arrOrnekNolar.length; i++) {
// i want to wait in here...
f_OrnekGetirKaydet(arrOrnekNolar[i], iKlinikId, bKaydet);
}
}
}
是否可以?
KR,
恰金
I want to lock other threads when one thread is running. It is possible in .Net with a lock object. I need do the same in AJAX.
function f_OrnekleriGetir(bKaydet) {
var ddlKlinikId = "<%=ddlKliniklker.ClientID%>";
var iKlinikId = $("#" + ddlKlinikId + " option:selected").val();
var arrOrnekNolar = new Array();
$(".tdOrnekNo").each(function(idx, td) {
var sOrnekNo = $(td).html();
arrOrnekNolar[idx] = sOrnekNo;
});
for (var i = 0; i < arrOrnekNolar.length; i++) {
// i want to wait in here...
f_OrnekGetirKaydet(arrOrnekNolar[i], iKlinikId, bKaydet);
}
}
}
Is it possible?
KR,
Çağın
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以用循环“锁定”主线程。不过,这里有一些主要缺点。
如果您想在等待 Ajax 请求时锁定线程,您可以同步发送请求。然而,这将具有与阻塞循环相同的缺点。
正如 MunkiPhD 所说,您可能正在查看问题不正确。回调在 JavaScript 中被广泛使用,以避免阻塞 UI 线程,几乎可以保证,构建代码以在计时器或事件上使用回调函数会比您当前的想法更好。
You can "lock" the main thread with a loop. There's a few major downsides here, though.
If you want to lock the thread whilst waiting for an Ajax request, you could send the request synchronously. This will have the same downsides as the blocking loop, however.
As MunkiPhD said, you're probably looking at the issue incorrectly. Callbacks are widely used in JavaScript to avoid blocking the UI thread, it's almost guaranteed that structuring your code to use a callback function on either a timer or an event will work much better than what you currently have in mind.
你不能在 javascript 中进行多线程处理。都是单线程的。
You can't do multithreading in javascript. It's all single-threaded.
我不确定是否可以在 Javascript 中锁定线程,但我认为这可以归结为设计问题。线程应该为可以并行运行的进程运行。如果您希望一个线程等待另一个线程的响应,那么您就错误地看待了这个问题。
不管怎样,您调查过Web Workers吗?
I'm not sure if you can lock threads in Javascript, but I think it boils down to a design issue. Threads are supposed to be run for processes that can run in parallel. If you want a thread to wait for a response from another thread, then you're looking at the issue incorrectly.
Either way, have you investigated Web Workers?