为什么setInterval只执行一次?

一代小雄 2016-12-02 10:32:54
<script type="text/javascript">
function direct() {
$.ajax({
type : "POST",
url : "<%=request.getContextPath()%>/log.do",
});
}
function showlog() {
$("#fm").submit();
setInterval(direct(), 1000);
}
</script>


通过onclick="showlog()"后,setInterval()这个只执行了一次,请大神指教~~~~

成功立即给分!
...全文
1073 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
当作看不见 2016-12-02
  • 打赏
  • 举报
回复

function direct(){
	alert(1);
}
setInterval(direct,1000);
当作看不见 2016-12-02
  • 打赏
  • 举报
回复
引用 5 楼 xiong9999 的回复:
[quote=引用 1 楼 qq_29594393 的回复:] setInterval(direct, 1000);//去掉括号就好
试了,去掉后这个定时器就没执行,没有执行direct()这个函数。。。。[/quote] setInterval(direct,1000); 没有问题 上面的那个我的大写i可能打错了成了小写L:了
一代小雄 2016-12-02
  • 打赏
  • 举报
回复
引用 1 楼 qq_29594393 的回复:
setInterval(direct, 1000);//去掉括号就好
试了,去掉后这个定时器就没执行,没有执行direct()这个函数。。。。
forwardNow 2016-12-02
  • 打赏
  • 举报
回复
1. 参考:《JavaScript权威指南第6版》第14章 14.1节 计时器 2. 预览:点击我进行访问

    function direct() {
        console.info( "time: ", ( new Date() ).getTime() );
    }
    function showlog() {
        setInterval(direct(), 1000);
    }
    function showlog_2() {
        setInterval( direct, 1000 );
    }
    function showlog_3() {
        setInterval( function () {
            direct();
        }, 1000 );
    }
    function showlog_4() {
        setInterval( "direct()", 1000 );
    }
    // showlog(); //=> 执行一次
    // showlog_2(); //=> 每隔 1000毫秒 执行一次
    // showlog_3(); //=> 每隔 1000毫秒 执行一次
    // showlog_4(); //=> 每隔 1000毫秒 执行一次
david___ 2016-12-02
  • 打赏
  • 举报
回复
setInterval的语法:
var intervalID = window.setInterval(func, delay[, param1, param2, ...]);
var intervalID = window.setInterval(code, delay);
当作看不见 2016-12-02
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/392043312 可以看看
当作看不见 2016-12-02
  • 打赏
  • 举报
回复
setInterval(direct, 1000);//去掉括号就好
xw4501339 2016-12-02
  • 打赏
  • 举报
回复
引用 15 楼 xw4501339 的回复:
[quote=引用 14 楼 chinmo 的回复:] [quote=引用 13 楼 xiong9999 的回复:] [quote=引用 12 楼 showbo 的回复:] 你现在不就是ajax提交了,你的$("#fm").submit(); 从哪搞出来的,代码贴全来
我写了两个ajax函数来进行提交:
$(function() {
		$("#send").click(function() {
			showlog();
			$.ajax({
				url : "<%=request.getContextPath()%>/addData.do",
				type : "POST",
				dataType : "json",
				data : $("#fm").serialize(),
			})
		});
	});
	function showlog() {
		setInterval(function() {
			$.ajax({
				url : "<%=request.getContextPath()%>/log.do",
				type : "POST",
			});
		}, 1000);
	}
这样都两个都可以提交到后台,现在是不知道提交到后台action后的return 是什么了, 如果直接用submit的话,可以返回一个jsp页面的呀[/quote] 刷新页面,没有了点击事件,你的自然就没有在执行了[/quote]

    $(function() {
        $("#send").click(function() {
            showlog();
        });
    });
    function showlog() {
        setInterval(function() {
            $.ajax({
                type : "POST",
                data:$("#fm").serialize(),
                url : "<%=request.getContextPath()%>/log.do",
                dataType : "json",
                success:function(msg){
                    showlog();
                },
                error:function(e){
                    showlog();
                }
            });
        }, 1000);
    }
[/quote] 修改一下

    $(function() {
        $("#send").click(function() {
            showlog();
        });
    });
    function showlog() {
        $.ajax({
            type : "POST",
            data:$("#fm").serialize(),
            url : "<%=request.getContextPath()%>/log.do",
            dataType : "json",
            success:function(msg){
                setTimeout(function(){
                    showlog();
                }, 1000);
            },
            error:function(e){
                setTimeout(function(){
                    showlog();
                }, 1000);
            }
        });    
    }
xw4501339 2016-12-02
  • 打赏
  • 举报
回复
引用 14 楼 chinmo 的回复:
[quote=引用 13 楼 xiong9999 的回复:] [quote=引用 12 楼 showbo 的回复:] 你现在不就是ajax提交了,你的$("#fm").submit(); 从哪搞出来的,代码贴全来
我写了两个ajax函数来进行提交:
$(function() {
		$("#send").click(function() {
			showlog();
			$.ajax({
				url : "<%=request.getContextPath()%>/addData.do",
				type : "POST",
				dataType : "json",
				data : $("#fm").serialize(),
			})
		});
	});
	function showlog() {
		setInterval(function() {
			$.ajax({
				url : "<%=request.getContextPath()%>/log.do",
				type : "POST",
			});
		}, 1000);
	}
这样都两个都可以提交到后台,现在是不知道提交到后台action后的return 是什么了, 如果直接用submit的话,可以返回一个jsp页面的呀[/quote] 刷新页面,没有了点击事件,你的自然就没有在执行了[/quote]

    $(function() {
        $("#send").click(function() {
            showlog();
        });
    });
    function showlog() {
        setInterval(function() {
            $.ajax({
                type : "POST",
                data:$("#fm").serialize(),
                url : "<%=request.getContextPath()%>/log.do",
                dataType : "json",
                success:function(msg){
                    showlog();
                },
                error:function(e){
                    showlog();
                }
            });
        }, 1000);
    }
  • 打赏
  • 举报
回复
引用 13 楼 xiong9999 的回复:
[quote=引用 12 楼 showbo 的回复:] 你现在不就是ajax提交了,你的$("#fm").submit(); 从哪搞出来的,代码贴全来
我写了两个ajax函数来进行提交:
$(function() {
		$("#send").click(function() {
			showlog();
			$.ajax({
				url : "<%=request.getContextPath()%>/addData.do",
				type : "POST",
				dataType : "json",
				data : $("#fm").serialize(),
			})
		});
	});
	function showlog() {
		setInterval(function() {
			$.ajax({
				url : "<%=request.getContextPath()%>/log.do",
				type : "POST",
			});
		}, 1000);
	}
这样都两个都可以提交到后台,现在是不知道提交到后台action后的return 是什么了, 如果直接用submit的话,可以返回一个jsp页面的呀[/quote] 刷新页面,没有了点击事件,你的自然就没有在执行了
一代小雄 2016-12-02
  • 打赏
  • 举报
回复
引用 12 楼 showbo 的回复:
你现在不就是ajax提交了,你的$("#fm").submit(); 从哪搞出来的,代码贴全来
我写了两个ajax函数来进行提交:
$(function() {
		$("#send").click(function() {
			showlog();
			$.ajax({
				url : "<%=request.getContextPath()%>/addData.do",
				type : "POST",
				dataType : "json",
				data : $("#fm").serialize(),
			})
		});
	});
	function showlog() {
		setInterval(function() {
			$.ajax({
				url : "<%=request.getContextPath()%>/log.do",
				type : "POST",
			});
		}, 1000);
	}
这样都两个都可以提交到后台,现在是不知道提交到后台action后的return 是什么了, 如果直接用submit的话,可以返回一个jsp页面的呀
Go 旅城通票 2016-12-02
  • 打赏
  • 举报
回复
你现在不就是ajax提交了,你的$("#fm").submit(); 从哪搞出来的,代码贴全来
一代小雄 2016-12-02
  • 打赏
  • 举报
回复
引用 10 楼 showbo 的回复:
[quote=引用 8 楼 xiong9999 的回复:] [quote=引用 7 楼 qq_29594393 的回复:]

function direct(){
	alert(1);
}
setInterval(direct,1000);
这个我试了,可以执行, 我知道我的代码为什么不执行了, $("#fm").submit(); 这个是form表单,执行submit后,jsp页面就已经刷新过了,所以只能是执行一次 我想在
<input id="send" type="button" value="Send" onclick="showlog()">
执行点击后,同时执行
$("#fm").submit(); 
setInterval(direct(), 1000);
用ajax或者其他,能实现这个功能吗?[/quote]改ajax提交,直接form.submit()提交后都刷新页面了,计时器自然无法继续执行[/quote] 直接用ajax提交怎么写呢?我不太会用..... 这个是我的js内容,我原有的ajax里要定时发送请求,怎么把这个表单的提交在这里写呢?
	$(function() {
		$("#send").click(function() {
			showlog();
		});
	});
	function showlog() {
		setInterval(function() {
			$.ajax({
				type : "POST",
				data:$("#fm").serialize(),
				url : "<%=request.getContextPath()%>/log.do",
			});
		}, 1000);
	}
Go 旅城通票 2016-12-02
  • 打赏
  • 举报
回复
引用 8 楼 xiong9999 的回复:
[quote=引用 7 楼 qq_29594393 的回复:]

function direct(){
	alert(1);
}
setInterval(direct,1000);
这个我试了,可以执行, 我知道我的代码为什么不执行了, $("#fm").submit(); 这个是form表单,执行submit后,jsp页面就已经刷新过了,所以只能是执行一次 我想在
<input id="send" type="button" value="Send" onclick="showlog()">
执行点击后,同时执行
$("#fm").submit(); 
setInterval(direct(), 1000);
用ajax或者其他,能实现这个功能吗?[/quote]改ajax提交,直接form.submit()提交后都刷新页面了,计时器自然无法继续执行
一代小雄 2016-12-02
  • 打赏
  • 举报
回复
引用 4 楼 wuqinfei_cs 的回复:
1. 参考:《JavaScript权威指南第6版》第14章 14.1节 计时器 2. 预览:点击我进行访问

    function direct() {
        console.info( "time: ", ( new Date() ).getTime() );
    }
    function showlog() {
        setInterval(direct(), 1000);
    }
    function showlog_2() {
        setInterval( direct, 1000 );
    }
    function showlog_3() {
        setInterval( function () {
            direct();
        }, 1000 );
    }
    function showlog_4() {
        setInterval( "direct()", 1000 );
    }
    // showlog(); //=> 执行一次
    // showlog_2(); //=> 每隔 1000毫秒 执行一次
    // showlog_3(); //=> 每隔 1000毫秒 执行一次
    // showlog_4(); //=> 每隔 1000毫秒 执行一次
这个我试了,也是可以执行的, 我知道我的代码为什么不执行了, $("#fm").submit(); 这个是form表单,执行submit后,jsp页面就已经刷新过了,所以只能是执行一次 我想在
<input id="send" type="button" value="Send" onclick="showlog()">
执行点击后,同时执行
$("#fm").submit(); 
setInterval(direct(), 1000);
用ajax或者其他,能实现这个功能吗?
一代小雄 2016-12-02
  • 打赏
  • 举报
回复
引用 7 楼 qq_29594393 的回复:

function direct(){
	alert(1);
}
setInterval(direct,1000);
这个我试了,可以执行, 我知道我的代码为什么不执行了, $("#fm").submit(); 这个是form表单,执行submit后,jsp页面就已经刷新过了,所以只能是执行一次 我想在
<input id="send" type="button" value="Send" onclick="showlog()">
执行点击后,同时执行
$("#fm").submit(); 
setInterval(direct(), 1000);
用ajax或者其他,能实现这个功能吗?

87,904

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧