由于我无法理解的原因,以下功能似乎不起作用。
function timerTick()
{
var t=setTimeout(timerTick,1000);
}
Run Code Online (Sandbox Code Playgroud)
一切都应该正常工作,但是当我调用该函数时,控制台只是说“未定义”。
想法?
我试图让 onbeforeunload 使用各种设置的计时器,但是当返回到位时,我似乎无法启动它。我希望它可以与计时器一起使用,但由于某种原因计时器无法正常工作。这是我正在工作的代码,感谢您帮助查看它,非常感谢。
var validNavigation = false;
function wireUpEvents() {
var leave_message = 'Leaving the page';
jQuery(
function goodbye() {
jQuery(window).bind('onbeforeunload', function() {
setTimeout(function() {
setTimeout(function() {
jQuery(document.body).css('background-color', 'red');
}, 10000);
},1);
return leave_message;
});
});
function leave() {
if(!validNavigation) {
killSession();
}
}
//set event handlers for the onbeforeunload and onunloan events
window.onbeforeunload = goodbye;
window.onunload=leave;
}
// Wire up the events as soon as the DOM tree is ready
jQuery(document).ready(function () {
wireUpEvents();
});
Run Code Online (Sandbox Code Playgroud) 我试图以下面的方式超时httpurlconnection
URL urlConnect = new URL(url.toString());
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection urlc = (HttpURLConnection) urlConnect.openConnection();
urlc.setConnectTimeout(1000*5);
urlc.setReadTimeout(1000*5);
urlc.connect();
Run Code Online (Sandbox Code Playgroud)
但无法连接,它将apache tomcat分配的超时时间超过2分钟而不是5秒时我提供了gthat,在这种情况下如何手动超时httpurlconnection
授予以下代码:
function updateOdometers(odometers) {
setTimeout(function(){
odometers[1].update(odometers[1].value + 10);
}, 500);
}
setInterval(updateOdometers(odometers), 2000);
Run Code Online (Sandbox Code Playgroud)
无论出于何种原因,此代码仅更新里程表的值一次,而不是内部延迟每2000ms更新一次.谷歌搜索/ SO-ing并没有给我带来太多结果.有任何想法吗?
我尝试每隔2秒输出一个警告框,其中包含"hello"消息,但只有5次.所以我写了这段代码:
var counter = 1;
do {
setTimeout
(
function()
{
alert("hello");
counter++;
},
2000
);
} while( counter <= 5 );
Run Code Online (Sandbox Code Playgroud)
但我的页面每次都在崩溃?为什么?什么是在警报之间添加延迟2000毫秒的最佳方法?
我想根据时间在div中添加和删除一个类.它应该在6秒后添加一个类,并在4秒后将其删除.我尝试了一个基本的实现.为什么这不起作用?我假设问题是两个setTimeout一起像这样.如果我注释掉第二行,那么第一行就可以了.这里发生了什么?
setTimeout(addHighlight(), 6000);
setTimeout(removeHighlight(), 10000);
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我如何在一个delay参数的基础上编写一个同时执行这两个功能(添加和删除类)的函数吗?
这是我在控制台中运行的代码.
// Parameterless arrow functions that are visually easier to parse
setTimeout( () => {
console.log('I happen sooner');
setTimeout( () => {
// deeper code
console.log('I happen later');
}, 1);
}, 1);
Run Code Online (Sandbox Code Playgroud)
这样的记录是这样的
32
I happen sooner
I happen later
Run Code Online (Sandbox Code Playgroud)
我不明白这是什么意思,每次增加2,我运行相同的代码.
我们有一个需要每秒更新的显示板.对服务器的AJAX调用触发存储过程,这是一个非常简单的SELECT语句,执行只需几毫秒.
最初,由于网络延迟(或太阳黑子,或谁知道什么),这个AJAX过程会(偶尔,很少)超时.通过调整我们如何处理计时器并将其设置timeout为0,我们已经拥有它,所以它现在运行稳定,超时永远不会发生......
话虽如此,我仍然担心暂停仍可能发生.如果发生,目标是它会继续前进.基本上,忽略超时,再试一次......永远.没有MaxError,RetryLimit或TryCount等.
这就是我现在拥有的:
setTimeout(function run() {
// When the timer elapses, get the data from the server
GetData();
setTimeout(run, _refreshRate);
}, 1000);
function GetData() {
//console.log("Attempting to obtain the data...");
jQuery.ajax({
url: "something.ashx",
type: "GET",
contentType: 'application/json; charset=utf-8',
success: function(resultData) {
//console.log("Got the data.");
ParseJson(resultData);
// show the last refresh date and time
$('#refreshTime').html(GetDateTime());
},
error : function(xhr, textStatus, errorThrown) {
if (textStatus == 'timeout') {
//console.log("Timeout occured while getting data from the server. Trying …Run Code Online (Sandbox Code Playgroud) 我是新的反应,我试图在1秒后改变状态
export class Header extends Component {
constructor() {
super()
this.state = {name: 'Will'}
}
render() {
setTimeout(() => {
this.setState({ name: 'Bob' })
}, 2000);
return (
<h1>
{this.state.name}
</h1>
)
}
}
Run Code Online (Sandbox Code Playgroud)
但是给了我这个警告
警告:只能更新已安装或安装的组件.这通常意味着您在已卸载的组件上调用了setState,replaceState或forceUpdate.这是一个无操作.请检查Header组件的代码.
我正在尝试使用HTML输入框设置超时,并将其用作参数setTimeout.
这是一个非常简单的任务,但我不是JS开发人员,因此我不确定如何将参数正确传递给箭头函数.
HTML代码仅包含2个元素.
<button id="myElementId">Click me</button>
<input id="milliseconds" type="number" placeholder="number of milliseconds"/>
Run Code Online (Sandbox Code Playgroud)
我的JS代码看起来像:
const clickbutton = document.getElementById('myElementId');
const millis = parseInt(document.getElementById('milliseconds').value, 10);
clickbutton.addEventListener('click', (event) => {
setTimeout(() => { alert("I work");
}, millis); // <-- here I don't want to use a static value
});
Run Code Online (Sandbox Code Playgroud)
但是好像我无法millis从箭头函数中的外部范围获取值.
我也试过传递第二个参数:
const clickbutton = document.getElementById('myElementId');
const millis = parseInt(document.getElementById('milliseconds').value, 10);
clickbutton.addEventListener('click', (event, millis) => {
setTimeout(() => { alert("I work");
}, millis);
});
Run Code Online (Sandbox Code Playgroud)
那里没有运气.
任何帮助表示赞赏.
谢谢.
settimeout ×10
javascript ×9
jquery ×2
ajax ×1
ecmascript-6 ×1
java ×1
reactjs ×1
setinterval ×1
timeout ×1
timer ×1
undefined ×1
while-loop ×1