据我所知,这两个javascript的行为方式相同:
选项A:
function myTimeoutFunction()
{
doStuff();
setTimeout(myTimeoutFunction, 1000);
}
myTimeoutFunction();
Run Code Online (Sandbox Code Playgroud)
选项B:
function myTimeoutFunction()
{
doStuff();
}
myTimeoutFunction();
setInterval(myTimeoutFunction, 1000);
Run Code Online (Sandbox Code Playgroud)
使用setTimeout和setInterval有什么区别吗?
当屏幕以react-native加载时,我想从3倒数到1.我像这样用setTimeOut尝试过它并没有用.我在这做错了什么?我怎样才能做到这一点?加载屏幕时,我想显示3 = - > 2 ==> 1,间隔为1秒.这是我的代码.
constructor(props) {
super(props);
this.state = {
timer: 3
}
}
// componentDidMount
componentDidMount() {
setTimeout(() => {
this.setState({
timer: --this.state.timer
})
}, 1000);
}
Run Code Online (Sandbox Code Playgroud) 我试图在我的Spring-MVC Web App中实现长轮询,但是在4-5继续AJAX请求之后它冻结了我的浏览器和其他请求.我不知道这里是什么是我的相关代码.
控制器方法:(服务器端): -
@Asynchronous
@RequestMapping("/notify")
public @ResponseBody
Events notifyEvent(HttpServletRequest request) {
Events events = null;
try {
events = (Events) request.getSession(false).getServletContext().getAttribute("events");
System.out.println("Request Came from" + ((com.hcdc.coedp.safe.domain.User) request.getSession(false).getAttribute(Constants.KEY_LOGGED_IN_USER)).getLoginId());
if (!events.getTypeOfEvents().isEmpty()) {
System.out.println("Removing older entries");
events.getTypeOfEvents().clear();
}
while (!events.isHappend()) {
//Waiting for event to happen.
}
events = Events.getInstance();
events.setHappend(false);
request.getSession(false).getServletContext().setAttribute("events", events);
}catch (Exception e) {
e.printStackTrace();
}
return events;
}
Run Code Online (Sandbox Code Playgroud)
长轮询脚本(客户端): -
$(document).ready(function() {
$.ajaxSetup({
async:true//set a global ajax requests as asynchronus
});
alert('Handler for .onload() called.'); …
Run Code Online (Sandbox Code Playgroud) 我创建了一个以一个图像结束的图像滑块,但现在我想更进一步,让它循环.
这是头标记中的代码
<style>
#picOne, #picTwo, #picThree, #picFour, #picFive{
position:absolute;
display: none;
}
#pics {
width:500px;
height:332px;
}
</style>
<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#picOne').fadeIn(1500).delay(3500).fadeOut(1500);
$('#picTwo').delay(5000).fadeIn(1500).delay(3500).fadeOut(1500);
$('#picThree').delay(10000).fadeIn(1500).delay(3500).fadeOut(1500);
$('#picFour').delay(15000).fadeIn(1500).delay(3500).fadeOut(1500);
$('#picFive').delay(20000).fadeIn(1500).delay(3500);
});
</script>
Run Code Online (Sandbox Code Playgroud)
这是在正文代码中实现的地方
<div id="pics">
<center>
<img src="img/dolllay.jpg" width="500" height="332" id="picFive" />
<img src="img/dye.jpg" width="500" height="332" id="picTwo" />
<img src="img/dollsit.jpg" width="500" height="332" id="picThree" />
<img src="img/heirloom.jpg" width="500" height="332" id="picFour" />
<img src="img/heritage.jpg" width="500" height="332" id="picOne" />
</center>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以把它变成一个函数然后循环吗?我可以得到任何指导吗?非常感谢你
当鼠标悬停在html元素上时,如何在鼠标悬停时调用函数
例:
<script>
function a() {
"special code hear"
}
</script>
<div onmouseover( 'a()')> </div>
Run Code Online (Sandbox Code Playgroud)
当鼠标悬停在div上而不是让它调用一次函数时,我怎么能继续调用函数.
我只想1次运行该功能.
timerA = setInterval(function()
{
//codes..
clearInterval(timerA);
}, 2000);
Run Code Online (Sandbox Code Playgroud)
我想只调用一次setInterval中的函数.我怎么能用setInterval和clearInterval呢?
还是有其他技术可以做到吗?
我试图每隔5秒更改一个组件的状态,如下面的componentDidMount()挂钩
import React, { Component } from 'react';
export default class ToTest extends Component {
constructor(props) {
super(props);
this.state = {
test: false
};
}
componentDidMount() {
setTimeout(() => { this.setState({ test: !this.state.test }) }, 5000);
}
renderDiv() {
if(this.state.test) {
return (<div>test is true</div>)
}
else {
return (<div>test is false</div>)
}
}
render() {
return (
<div>{ this.renderDiv() }</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
但它只执行一次.它从false变为true,然后没有变化.我错过了什么?
我试图根据我向下滚动页面的方向在y方向沿着一个大div移动一个小div.但我发现使用setTimeout()和setInterval()会得到两个完全不同的结果.实际上是setInterval( )被浏览器多次绞死.这两个函数的基本区别是什么?
<!DOCTYPE html>
<head>
<title>creat a dynamic div</title>
<style>
#mydiv{
border:2px solid green;
}
</style>
</head>
<body>
<script>
var i=0;
var elem1=document.createElement("div");
var atts1=document.createAttribute("style");
atts1.value="width:200px;height:3200px;border:1px solid black;background-color:orange;";
elem1.setAttributeNode(atts1);
document.body.appendChild(elem1);
var elem2=document.createElement("div");
var atts2=document.createAttribute("style");
var atts22=document.createAttribute("id");
atts22.value="mydiv";
atts2.value="width:200px;height:300px;background-color:red;position:absolute;top:0px;left:300px;";
elem2.setAttributeNode(atts2);
elem2.setAttributeNode(atts22);
document.body.appendChild(elem2);
function moveIt(){
var a=window.pageYOffset;
if(i > (a+30)){
clearTimeout(p);
}else{
elem2.style.top=i+"px";
i=i+1;
}
var p=setTimeout(moveIt,200);
}
window.onscroll=moveIt;
</script>
</body>
<html>
Run Code Online (Sandbox Code Playgroud) javascript ×8
jquery ×4
setinterval ×2
settimeout ×2
ajax ×1
css ×1
function ×1
html ×1
java ×1
long-polling ×1
react-native ×1
reactjs ×1
rotator ×1
slider ×1
spring-mvc ×1