gpt4 book ai didi

javascript - 在 windows 8 html5 的挂起状态事件处理程序中写入文本文件

转载 作者:行者123 更新时间:2023-11-30 10:31:26 25 4
gpt4 key购买 nike

我正在尝试在触发挂起事件时写入一个文本文件,即在 winjs.application.oncheckpoint 事件处理程序中。我正在将我的对象写成 JSON 文本。这是代码:

        applicationData.localFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (sampleFile) {
var stringData = "";
var i;
for (i = 0 ; i < myData.objData.length - 1 ; i++) {
stringData += '{"title":"' + myData.objData[i].title + '","challange":"' + myData.objData[i].challange + '"},\n';
}
stringData += '{"title":"' + myData.objData[i].title + '","challange":"' + myData.objData[i].challange + '"}';
stringData = "[" + stringData + "]";
return Windows.Storage.FileIO.writeTextAsync(sampleFile, stringData);
}).done(function () { });

但 Windows 应用程序在将任何内容写入文本文件之前关闭。我调用了 args.setPromise() 并将上述代码作为函数参数传递,但同样的问题仍然存在。

PS:我不知道如何正确地异步执行它。请帮忙。

最佳答案

就像 Phil 评论的那样,删除 .done() 将修复它。

这是因为 done() 不返回任何内容,而 then() 返回链式 promise 。这就是什么都没有被调用的原因。

还建议在应用程序数据发生变化时或每隔一段时间就保存您的应用程序数据,而不是等待挂起事件来保存所有内容。暂停事件给出 5s 的时间。否则,应用程序将根据 msdn documentation here 终止。 .

“如果应用程序未在 5 秒内从挂起事件中返回,Windows 会假定该应用程序已停止响应并终止它”

app.oncheckpoint = function (args) {
app.sessionState.history = nav.history;

args.setPromise(Windows.Storage.ApplicationData.current.localFolder.createFileAsync
("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (sampleFile) {
var stringData;
// code to set stringData.

return Windows.Storage.FileIO.writeTextAsync(sampleFile, stringData);
}));

};

关于javascript - 在 windows 8 html5 的挂起状态事件处理程序中写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16749112/

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