我有两个JS函数.一个叫另一个.在调用函数中,我想调用另一个,等待该函数完成,然后继续.所以,例如/伪代码:
function firstFunction(){
for(i=0;i<x;i++){
// do something
}
};
function secondFunction(){
firstFunction()
// now wait for firstFunction to finish...
// do something else
};
Run Code Online (Sandbox Code Playgroud)
我提出了这个解决方案,但不知道这是否是一个聪明的方法.
var isPaused = false;
function firstFunction(){
isPaused = true;
for(i=0;i<x;i++){
// do something
}
isPaused = false;
};
function secondFunction(){
firstFunction()
function waitForIt(){
if (isPaused) {
setTimeout(function(){waitForIt()},100);
} else {
// go do that thing
};
}
};
Run Code Online (Sandbox Code Playgroud)
这是合法的吗?是否有更优雅的方式来处理它?也许用jQuery?
我怎样才能为用户提供5秒的时间来写一些东西,以便要求无限期的暂停.如果在这5秒内没有要求暂停,则该过程继续.如果需要暂停,则用户具有他需要的所有时间以及他可以点击"输入"以便随时恢复该过程.
这种功能的兴趣在于如果用户不在,则暂停仅持续5秒.并且如果用户在场,则他可以享受暂停以便观看例如已经产生的图表.
代码最终可能看起来像这样:
DoYouWantaPause = function(){
myprompt = "You have 5 seconds to write the letter <p>. If you don't the process will go on."
foo = readline(prompt = myprompt, killAfter = 5 Seconds) # give 5 seconds to the user. If the user enter a letter, then this letter is stored in `foo`.
if (foo == "p" | foo == "P") { # if the user has typed "p" or "P"
foo = readline(prompt = "Press enter when you …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用HTML5画布,JavaScript和XML制作一种游戏.我们的想法是,您可以通过将问题和答案放在XML文件中来进行测验.我写了一个主循环,循环遍历所有问题,构建它们并检查答案的正确性.现在我只是使用警报和对话框来回答问题问题是我的主循环是一个大的互连整体,从头到尾遍历整个游戏,而不是让警报框提出问题和对话框回答,紧接着,我想要一些用户互动.问题的答案显示在屏幕底部的框中,用户可以控制起重机选择正确的答案.这是我坚持的主循环代码片段:
answer = loadQuestion(i);
if (answer == "correct") {
// answered correctly, update scoreArray and load next question
scoreArray[currentQuestion] = "correct";
// show 'next'-button and wait for press to continue
} else {
// answered incorrectly again, update scoreArray and load next question
scoreArray[currentQuestion] = "error";
// show 'next'-button and wait for press to continue
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在调用loadQuestion,它会立即加载问题,显示可能的答案,现在立即抛出一个对话框,您可以在其中键入答案.此答案将被返回并验证.
我已经编程了起重机的控制装置,用户已经可以拿起一个装有它的盒子.但是因为我正在调用loadQuestion并期望它返回一个值,所以这不起作用.如果玩家使用起重机给出答案,我该如何让我的主循环"暂停",然后继续?我已经尝试将答案作为一个全局变量,并且只是在答案==""之前有空,以便在答案获得值之前保持函数忙无所事事,但这只是冻结了脚本.我还试图监视应答变量的状态,并且在发生这种情况时清除间隔并返回值,但是这只是返回false,因为函数完成而没有立即返回值.
我有这个功能,只要我点击一个按钮,就会以00:00:00的格式启动计时器.但我不知道如何做功能恢复和暂停.我发现了一些我认为可能有用的片段,但我无法完成这些工作.我是新手在js中使用对象.
function clock() {
var pauseObj = new Object();
var totalSeconds = 0;
var delay = setInterval(setTime, 1000);
function setTime() {
var ctr;
$(".icon-play").each(function () {
if ($(this).parent().hasClass('hide')) ctr = ($(this).attr('id')).split('_');
});
++totalSeconds;
$("#hour_" + ctr[1]).text(pad(Math.floor(totalSeconds / 3600)));
$("#min_" + ctr[1]).text(pad(Math.floor((totalSeconds / 60) % 60)));
$("#sec_" + ctr[1]).text(pad(parseInt(totalSeconds % 60)));
}
}
Run Code Online (Sandbox Code Playgroud)
pad()只是添加前导零
在MATLAB中,我正在运行一些需要一段时间才能运行的代码.我想暂停代码来检查一些变量值.有没有办法我可以做到这一点,而无需从头开始重新运行代码?我不想终止该计划; 只是暂停它.
我开始使用ConcellationTokenSource和CancellationToken在框架4.0中提供的协作线程取消模式,我发现它非常有用和简单.
我的目的是为我的应用程序提供类似的优雅和简单的解决方案,但暂停线程而不是取消它们.因为在这种情况下,请求将与监听pause命令不同,我认为像PauseTokenSource和PauseToken这样的东西会很好.所以我的第一个问题是,如果你建议合作暂停的这种模式,或者它是否更好的其他东西.
如果有这样的模式是个好主意,你对这个模式有什么建议或指导吗?目前我认为源应该能够通过ManualResetEvent暂停和取消暂停,并且令牌应该具有对源的引用.它遵循初稿,我希望你能给我一些改进建议.
public class PauseTokenSource
{
protected ManualResetEvent mre = new ManualResetEvent(true);
object syncRoot = new object();
public PauseToken PauseToken { get { return new PauseToken(this); } }
public bool IsPauseRequested { get { return !mre.WaitOne(0); } }
public void Pause()
{
mre.Reset();
}
public void UnPause()
{
mre.Set();
}
public void WaitUntillPaused()
{
mre.WaitOne();
}
}
public class PauseToken
{
private PauseTokenSource source;
public PauseToken(PauseTokenSource source)
{
this.source = source;
}
public bool IsPauseRequested
{
get { return …Run Code Online (Sandbox Code Playgroud) c# design-patterns pausing-execution cancellation cancellationtokensource
我使用基于此代码的关键帧动画整理了一个纯css文本幻灯片:https://codepen.io/johnlouie04/pen/BqyGb
我已经做到这一点,当你悬停过渡时,它会暂停.这在谷歌浏览器和Safari中完美运行,但是在Firefox上悬停滑块时,动画会在暂停前快速重放.即使没有animation-play-state:paused线也会发生这种情况.
滑块中还有另一个悬停选择器,它似乎也会导致动画重放.但是我删除哪一个并不重要,只要滑块内任何类型的悬停选择器(即使它与动画无关),Firefox会发生奇怪的事情.
我已经google了很长时间,找不到任何有同样问题的人.有谁知道如何解决这个问题?我会非常感激一些帮助.
这是代码:
<html>
<body>
<style>
#slider {
overflow: hidden;
position: relative;
width: 920px;
z-index: 0;
margin: 0 auto;
padding: 0;
}
#slider li {
float: left;
position: relative;
display: inline-block;
margin: 0px;
}
#slider-margin {
margin: 50px 0px;
padding: 0px;
border-radius: 8px;
border-bottom: 3px solid rgba(0,10,30,0.1);
position: relative;
background: rgba(0,10,30,0.2);
}
#slider li {
float: left;
position: relative;
display: inline-block;
margin: 0px;
}
#slider ul {
list-style: none;
position: relative;
left: 0px; …Run Code Online (Sandbox Code Playgroud) 我刚刚在C#中构建了一个C解释器,现在开始将它转换为Javascript.一切都很顺利,直到我意识到js没有睡眠功能.我的解释器使用递归解析器,它暂停用户输入,同时它嵌套了几个深层函数(在C#中我在第二个线程中使用了waithandle).我查看了setInterval和setTimeout,但它们是异步/非阻塞的; 当然,busywait是不可能的,我查看了我在SO上找到的timed_queue实现,但没有运气.我在主窗口和webworker中都尝试过解析器.我正在使用jQuery.我对js的经验有限,我正在寻找想法.我对继续传递风格或收益率知之甚少,我想知道他们是否可能持有关键.这里有一些从代码中删除来显示一些控件脚本.任何想法请...
var STATE = {
START: "START",
RUN: "RUN", //take continuous steps at waitTime delay
STEP: "STEP", //take 1 step
PAUSE: "PAUSE",//wait for next step command
STOP: "STOP",
ERROR: "ERROR"
}
var state = state.STOP;
function parsing_process() //long process we may want to pause or wait in
{
while(token !== end_of_file)//
{
//do lots of stuff - much of it recursive
//the call to getNextToken will be encountered a lot in the recursion
getNextToken();
if (state === STATE.STOP)
break; …Run Code Online (Sandbox Code Playgroud) javascript ×4
delay ×2
jquery ×2
c# ×1
cancellation ×1
css ×1
css3 ×1
debugging ×1
firefox ×1
html ×1
interpreter ×1
matlab ×1
r ×1
setinterval ×1