【发布时间】:2013-11-27 00:43:23
【问题描述】:
我正在使用一些可以添加一些自定义脚本的软件。经过反复试验,我发现这个脚本只有在我这样写的情况下才能工作:
setTimeout(function(){
//code
}, 900);
但我觉得这有点难看,我觉得应该可以做得更聪明。我也不知道在其他计算机上我是否可能需要更长的时间。也许对于其他人,我需要将其设置为 5000,而对于其他人,将其设置为 300 是可以的。
是否可以在加载完所有内容后触发该功能?
这种方法,我试过了,但没有奏效:
$(document).ready(function(){ .. });
document.addEventListener('DOMContentLoaded', function() {...}, false);
这是我的完整代码:
setTimeout(function(){
function correct(searchString, replaceString) {
$("textarea").bind("keyup", function () {
var $input = $(this),text = $input.val();
var pos = $(this).prop("selectionStart");
if (new RegExp(searchString).test(text.substring(pos-searchString.length,pos)) == true) {
var newText = text.substring(0,pos-searchString.length) + replaceString + text.substring(pos);
$input.val(newText);
var newpos = pos - searchString.length + replaceString.length;
this.setSelectionRange(newpos,newpos);
}
});
}
correct("RR", "ℝ");
correct(">=","≥");
}, 900);
我已将其设置为话语论坛 (discourse.org) 中的自定义标题
【问题讨论】:
-
你试过把它放在一个“准备好的”处理程序中吗?
$(function() { /* your code here */ }); -
@NiettheDarkAbsol 是的,不起作用。
-
你试过把它放在你的 jQuery 包含脚本之后吗?
-
@NiettheDarkAbsol 是的
标签: javascript function settimeout