有没有sleep比使用以下pausecomp函数更好的方法来设计JavaScript (从这里开始)?
function pausecomp(millis)
{
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis);
}
Run Code Online (Sandbox Code Playgroud)
这不是JavaScript中Sleep的重复- 动作之间的延迟 ; 我希望在函数中间实现真正的睡眠,而不是在执行代码之前的延迟.
下面的这个功能不像我想要的那样工作; 作为一个JS新手我无法弄清楚为什么.
我需要它等待5秒才能检查是否newState是-1.
目前,它不会等待,它只是立即检查.
function stateChange(newState) {
setTimeout('', 5000);
if(newState == -1) {
alert('VIDEO HAS STOPPED');
}
}
Run Code Online (Sandbox Code Playgroud) 我希望widget.Rotator.rotate()在调用之间延迟5秒...我如何在jQuery中执行此操作...似乎jQuery delay()不适用于此...
我想停止执行2秒钟.这是这样,但现在遵循一个代码块:
<html>
<head>
<title> HW 10.12 </title>
<script type="text/javascript">
for (var i = 1; i <= 5; i++) {
document.write(i);
sleep(2); //for the first time loop is excute and sleep for 2 seconds
};
</script>
</head>
<body></body>
</html>
Run Code Online (Sandbox Code Playgroud)
第一次循环是执行并睡眠2秒.我想停止执行两秒钟?
我正在用javascript编写基于文本的游戏,主要的“功能”之一是输入框,它接受用户输入,并通过button标签提交输入。在我的主游戏循环中,调用按钮的onclick:
var game_u = new game_util();
function Game_Main(){
while(active){
input = game_u.getText();
//p = new player();
active = false;
}
}
function game_util(){
this.getText = function(){
//The Submit button
confirm_plr.onclick = function(){
//Input value
return player_in.value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这种方式的问题是,while循环不会“等待”单击提交按钮以从game_u.getText()获取输入。功能并继续循环。
我有更好的方法吗?这是我第一次玩文字游戏?我不想使用提示方法,因为它破坏了游戏的沉浸感。
我也是来自Java(一种面向对象的编程语言),所以这就是为什么要使用while循环的原因。
任何帮助表示赞赏。
我如何转换以下内容:
while True:
# do something
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
进入javascript?
音频元素正在播放音量:
audio.setVolume(.20)
Run Code Online (Sandbox Code Playgroud)
在某一点上,我想淡出音量,而不是突然切断,所以本质上我想要
audio.setVolume(.15)
audio.setVolume(.10)
audio.setVolume(.05)
audio.setVolume(.03)
audio.setVolume(.01)
Run Code Online (Sandbox Code Playgroud)
但是在这些变化之间需要有一些非常短暂的延迟,所以它们是可听的,我得到了淡出效果.这样做的正确方法是什么?
谢谢!
我有执行的 javascript 函数,执行后我想等待 2 秒。在 Javascript 中是否可能。
我的问题不同。我想在函数执行或完成执行后等待,而不是等到函数执行为止。
JavaScript 函数
function ajax_closeCall(onDone) {
// alert("Close Call invoked.");
closeCall_onDone = onDone;
var closeCallUrl = soapUrl + "?action=closeCall&parentSessionId=" + parentSessionId;
closeCall_http_request = getNewHttpRequest('text/plain');
closeCall_http_request.onreadystatechange = callback_ajax_closeCall;
// http_request.open("POST", soapUrl, true);
closeCall_http_request.open("GET", closeCallUrl, true);
closeCall_http_request.send(null);
}
function callback_ajax_closeCall() {
if (closeCall_http_request.readyState != 4) {
return;
}
if (closeCall_http_request.status == 200) {
if (closeCall_onDone) {
closeCall_onDone();
}
stopMonitorCallState();
ajax_getCallState();
} else {
// there was a problem with the request,
// for example the …Run Code Online (Sandbox Code Playgroud) 我试图在 React 中创建一个排序算法可视化工具。此功能正在运行,但我想减慢 for 循环的速度,以便每 400 毫秒设置一次状态。
bubbleSort = (arr) => {
console.log('bubblesort is running');
var len = arr.length;
console.log('array length: ', len);
console.log(arr);
for (var i = len-1; i>=0; i--){
console.log("i: ", i);
for(var j = 1; j<=i; j++){
console.log("j: ", j);
if(arr[j-1]>arr[j]){
var temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
console.log("current array: ", arr);
this.setState({
array: arr
})
}
}
}
console.log("final array: ", arr)
return arr
}
Run Code Online (Sandbox Code Playgroud) 我想做这个
statement1;
// rest for 0.5 seconds
statement2;
Run Code Online (Sandbox Code Playgroud)
如何让我的JavaScript等待0.5秒?
我试过了
statement1;
window.setTimer(500);
statement2;
Run Code Online (Sandbox Code Playgroud)
但那不是它.谢谢