gpt4 book ai didi

javascript - JavaScript 中长时间运行的任务

转载 作者:行者123 更新时间:2023-12-03 12:41:32 25 4
gpt4 key购买 nike

我有以下用于 JavaScript 中长时间运行命令的代码。这有点类似于this thread 。但是我修改了代码以满足我的要求,但现在它似乎不起作用。有人可以建议如何在后台运行此命令时显示“正在处理请求,请等待..”类型的消息:

<html>
<head>
<title>JavaScript Remote Exec Test</title>
<script type="text/javascript">
function defer(func) {
var proc = function(){
func();
}
setTimeout(proc, 50);
}

function GetPC()
{
var strComputerName = "NA";
var outObj = document.getElementById("outputText");

try
{
var getPCNameCommand = new ActiveXObject("WScript.Shell");
var cmdPCName = getPCNameCommand.Exec("c:\\windows\\system32\\cmd.exe /c hostname");

strComputerName = cmdPCName.StdOut.ReadAll();
}
catch (ex)
{
strComputerName = "Can't retrive PC name: " + ex;
}

return strComputerName;
}

function LaunchApp()
{
var appPath = "c:\\windows\\system32\\Gpupdate.exe";
var errormessage = appPath;
var strResult = "";
var strComputerName = GetPC();
var outObj = document.getElementById("outputText");
outObj.innerHTML = "Status: Updating group policy for " + strComputerName + "...";

defer(UpdateGPO);
/* try
{
var gpuExec = new ActiveXObject("WScript.Shell");
var execResult = gpuExec.Exec(appPath);

while (execResult.Status == 0) // wait for the command to finish
{
outObj.innerHTML = "Updating group policy. \n\n For Computer:\t" + strComputerName;
}

strResult = execResult.StdOut.ReadAll();
strResult = strResult.replace(".","\n");
outObj.innerHTML = strResult;
}
catch (ex)
{
errMsg = errormessage + " could not be launched. \n\n" + ex;
outObj.innerHTML = errMsg;
}
*/
}
function UpdateGPO() {
try
{
var gpuExec = new ActiveXObject("WScript.Shell");
var execResult = gpuExec.Exec(appPath);

while (execResult.Status == 0) // wait for the command to finish
{
//outObj.innerHTML = "Updating group policy. \n\n For Computer:\t" + strComputerName + ". Please wait...";
sleep(5);
}

strResult = execResult.StdOut.ReadAll();
strResult = strResult.replace(".","\n");
outObj.innerHTML = strResult;
}
catch (ex)
{
errMsg = errormessage + " could not be launched. \n\n" + ex;
outObj.innerHTML = errMsg;
}
}
</script>
</head>
<body style="font-family:'Segoe UI';">
<input type="button" value="Update Group Policy" onclick="LaunchApp();"></input>
<p id="outputText">Waiting for command</p>
</body>
</html>

最佳答案

好的,让我看看是否可以解释一下这里的问题是什么......

        while (execResult.Status == 0) // wait for the command to finish
{
outObj.innerHTML = "Updating group policy. \n\n For Computer:\t" + strComputerName;
}

JavaScript 是单线程,这意味着它一次只能做一件事。 (目前,这是忽略网络 worker ,这不计入这个答案。)

所以..你正在做一个while循环,但你永远不会给浏览器“呼吸”的机会并更新execResult.Status,所以你真正拥有的是一个无限循环。

那里没有足够的代码让我重现问题,因此您必须处理解决方案的“猜测”:

function waitForComplete() {
// execResult is in scope...
if (execResult.Status == 0) // wait for the command to finish
{
outObj.innerHTML = "Updating group policy. \n\n For Computer:\t" + strComputerName;
} else {
window.setTimeout(waitForComplete, 250);
}
}


var gpuExec = new ActiveXObject("WScript.Shell");
var execResult = gpuExec.Exec(appPath);
waitForComplete();

关于javascript - JavaScript 中长时间运行的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23510902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com