请解释一下我做错了什么...... [英] Please explain what I'm doing wrong...

查看:50
本文介绍了请解释一下我做错了什么......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找解释为什么一段代码可以工作的原因和

另一段代码没有。我必须警告你,这是我写过的第一篇

Javascript,所以如果有更好的方法或更简单的答案,那么所有人都可以意思是给我看灯!


我想做的是定时刷新页面

(实际重定向页面...... )我有一段简单的代码我

从网上得到了,但我需要稍微修改它以便我可以

调用该函数并指定在重定向

页面之前需要等待的时间。如果那还不清楚,希望代码可以!


顺便说一下,感谢您的帮助 - 我真的很感激!

这里有.. ...


-----------------工作代码---------------- ----

< script language =" JavaScript">

<! -

var sTargetURL =" Index .php" ;;

var cacheBuster = parseInt(Math.random()* 9999999); //缓存清除

强制服务器加载新页面

函数doRedirect2(){setTimeout(" timedRedirect()", 1 * 700); } $ / $
函数timedRedirect(){window.location.href = sTargetURL +"?cb ="

+ cacheBuster; }

// - >

< / script>


//页面调用 - 工作选项1

< input

type =" button"

value =" Click me!"

onclick = " doRedirect()"

/>

< / form>


//页面调用 - 工作选项2

< script type =" text / javascript">

doRedirect()

< / script>


-----------------结束工作代码--------------------

这里是我想要工作的代码......


--------------- - 此代码需要帮助--------------------

< script language =" JavaScript">

<! -

函数redirectPage(interval,timeValueAbbr){


//检查空值'并设置默认值

if(interval == null){interval = 1; }

if(timeValueAbbr == null){timeValueAbbr =" ss" ;; }


//所有缩写都是MSSQL格式:参见B.O.L. for datepart()

if(timeValueAbbr ==" ss"){timeMultiplier = 1000; }

if(timeValueAbbr ==" mi"){timeMultiplier = 1000 * 60; }

if(timeValueAbbr ==" hh"){timeMultiplier = 1000 * 60 * 60; }

if(timeValueAbbr ==" dd"){timeMultiplier = 1000 * 60 * 60 * 24; } $ / $

//创建并填充所需的变量

var sTargetURL =" Index.php"

var waitTime = interval * timeMultiplier;

var cacheBuster = parseInt(Math.random()* 9999999); //缓存

破坏强制服务器加载新页面


函数doRedirect(){setTimeout(" timedRedirect()",1 * 700); } $ / $
函数timedRedirect(){window.location.href = sTargetURL +"?cb ="

+ cacheBuster; }


}

// - >

< / script>


//页面调用 - 不工作选项1

< input

type =" button"

value ="点击我!"

onclick =" redirectPage()"

/>

< / form>


//页面调用 - 不工作选项2

< script type =" text / javascript">

redirectPage()

< / script>


-----------------需要帮助的代码结束---- ----------------

我做错了什么?

I''m looking for an explanation of why one piece of code works and
another does not. I have to warn you that this is the first piece of
Javascript I''ve ever written, so if there is a better way or a simpler
answer, by all means show me the light!

What I''m trying to do is refresh the page at a timed interval
( actually redirect the page... ) and I have a simple piece of code I
got from the net that works, but I need to modify it a little so I can
call the function and specify the time to wait before redirecting the
page. If that''s not clear, hopefully the code will be!

btw, thanks for the help - I really appreciate it!
Here goes.....

----------------- WORKING CODE --------------------
<script language="JavaScript">
<!--
var sTargetURL = "Index.php";
var cacheBuster = parseInt(Math.random()*9999999); // "cache busting"
forces the server to load a ''new'' page
function doRedirect2() { setTimeout( "timedRedirect()", 1*700 ); }
function timedRedirect() { window.location.href = sTargetURL + "?cb="
+ cacheBuster; }
//-->
</script>

//Page Call - Working Option 1
<input
type="button"
value="Click me!"
onclick="doRedirect()"
/>
</form>

//Page Call - Working Option 2
<script type="text/javascript">
doRedirect()
</script>

----------------- END of WORKING CODE --------------------
Here''s the code that I would LIKE to have working...

----------------- THIS CODE NEEDS HELP --------------------
<script language="JavaScript">
<!--
function redirectPage(interval, timeValueAbbr) {

// check for null value''s and set defaults
if (interval == null) { interval = 1; }
if (timeValueAbbr == null) { timeValueAbbr = "ss"; }

// all abbreviations are in MSSQL format: see B.O.L. for datepart()
if (timeValueAbbr == "ss") { timeMultiplier = 1000; }
if (timeValueAbbr == "mi") { timeMultiplier = 1000 * 60; }
if (timeValueAbbr == "hh") { timeMultiplier = 1000 * 60 * 60; }
if (timeValueAbbr == "dd") { timeMultiplier = 1000 * 60 * 60 * 24; }

// create and populate needed variables
var sTargetURL = "Index.php"
var waitTime = interval * timeMultiplier;
var cacheBuster = parseInt( Math.random() * 9999999 ); // "cache
busting" forces the server to load a ''new'' page

function doRedirect() { setTimeout( "timedRedirect()", 1*700 ); }
function timedRedirect() { window.location.href = sTargetURL + "?cb="
+ cacheBuster; }

}
//-->
</script>

//Page Call - NOT Working Option 1
<input
type="button"
value="Click me!"
onclick="redirectPage()"
/>
</form>

//Page Call - NOT Working Option 2
<script type="text/javascript">
redirectPage()
</script>

----------------- END of CODE THAT NEEDS HELP --------------------
What am I doing wrong?

推荐答案

SirG说:
SirG said:

>
我正在寻找解释为什么一段代码有效的原因和
另一个没有。我必须警告你,这是我写过的第一篇Javascript,所以如果有更好的方法或更简单的答案,请务必向我展示光明! />
我想要做的是以定时间隔刷新页面
(实际上重定向页面......)我有一段简单的代码我来自工作的网,但我需要稍微修改它,以便我可以调用该函数并指定等待重定向页面之前等待的时间。如果那还不清楚,希望代码可以!

顺便说一下,感谢您的帮助 - 我真的很感激!
>
I''m looking for an explanation of why one piece of code works and
another does not. I have to warn you that this is the first piece of
Javascript I''ve ever written, so if there is a better way or a simpler
answer, by all means show me the light!

What I''m trying to do is refresh the page at a timed interval
( actually redirect the page... ) and I have a simple piece of code I
got from the net that works, but I need to modify it a little so I can
call the function and specify the time to wait before redirecting the
page. If that''s not clear, hopefully the code will be!

btw, thanks for the help - I really appreciate it!


>我做错了什么?
>What am I doing wrong?



如果你想做的就是在一定的秒数后自动重定向这个

页面,只需添加一个

行到<页面的头部:


< meta http-equiv =" refresh"含量= << 5; url = Index.php">


用你的时间间隔替换5秒。

但是,如果你想看到固定的Javascript解决方案

这样它可以工作:


你的第一个错误是从一个不良来源复制代码。

你永远不应该使用parseInt( )对于

数学运算的结果,并且不需要

" cacheBuster"无论如何,这个值都是一个整数。


然后你做了很多额外的工作来将时间转换为
的间隔和单位值到等待时间。 ,但从来没有

在间隔或单位中传递或使用waitTime

变量。


然后你有几个嵌套函数定义

除非是非法的,否则不会做任何事情。


以下代码重定向到在按钮的onClick处理程序中指定的秒数

后,在

sTargetURL中命名的URL。


< html> ;

< head>

< script type =" text / javascript">

function redirectPage(sec){

//创建并填充所需的变量

var sTargetURL =" Index.php"

var waitTime = sec * 1000;

var cacheBuster = Math.random();

//" cache busting"强制服务器加载新页面

setTimeout(" location.href =''" + sTargetURL +"?cb =" + cacheBuster +"''" ;,waitTime);

}

< / script>

< / head>

<正文>

<输入

类型="按钮"

value ="点击我!"

onclick =" redirectPage(5)"

If all you want to do is to automatically redirect this
page after a certain number of seconds, just add one
line to the <headsection of your page:

<meta http-equiv="refresh" content="5; url=Index.php">

Replace the 5 with your time interval in seconds.
But, if you want to see the Javascript solution fixed
so that it works:

Your first mistake was to copy code from a bad source.
You should never apply parseInt() to the result of a
mathematical operation, and there''s no need for the
"cacheBuster" value to be an integer, anyway.

Then you did a lot of extra work to convert a time
interval and units value to a "waitTime", but never
passed in the interval or units or used the waitTime
variable.

Then you had a couple of nested function definitions
that, besides being illegal, weren''t going to do
anything.

The following code redirects to the URL named in
sTargetURL after the number of seconds specified
in the onClick handler of the button.

<html>
<head>
<script type="text/javascript">
function redirectPage(sec) {
// create and populate needed variables
var sTargetURL = "Index.php"
var waitTime = sec * 1000;
var cacheBuster = Math.random();
// "cache busting" forces the server to load a ''new'' page
setTimeout( "location.href=''"+sTargetURL+"?cb="+cacheBuster+"'' ", waitTime );
}
</script>
</head>
<body>
<input
type="button"
value="Click me!"
onclick="redirectPage(5)"


>
>



< / form>

< / body>

< / html>

-

</form>
</body>
</html>
--


我正在寻找解释为什么一段代码有效并且
I''m looking for an explanation of why one piece of code works and

另一个没有。我必须警告你,这是我写过的第一篇

Javascript,所以如果有更好的方法或更简单的答案,那么所有人都可以意思是告诉我光明!
another does not. I have to warn you that this is the first piece of
Javascript I''ve ever written, so if there is a better way or a simpler
answer, by all means show me the light!



这可能会有所帮助......


< URL:http://groups.google.com/group /comp.lang.javascript/msg/be461c04829081ce>

This might be helpful...

<URL: http://groups.google.com/group/comp.lang.javascript/msg/be461c04829081ce>


< script language =" JavaScript">
<script language="JavaScript">



不推荐使用language属性。使用type =" text / javascript"

而不是

The language attribute is deprecated. Use type="text/javascript"
instead


<! -
<!--



代码评论技巧在1997年停止有用或类似

类似

The code commenting trick stopped being useful in 1997 or something
similar


function redirectPage(interval,timeValueAbbr){


//检查空值'并设置默认值

if(interval == null){interval = 1; }
function redirectPage(interval, timeValueAbbr) {

// check for null value''s and set defaults
if (interval == null) { interval = 1; }



如果(!interval){interval = 1;}

$ b $你可以写

b甚至


interval = interval || 1;

You can write
if (!interval) {interval = 1;}

or even

interval = interval || 1;


if(timeValueAbbr == null){timeValueAbbr =" ss" ;; }
if (timeValueAbbr == null) { timeValueAbbr = "ss"; }



如果你在if'的

下面使用默认情况,则无需上述行

No need for the above line if you use a default case in your if''s
below


//所有缩写都是MSSQL格式:请参阅BOL for datepart()

if(timeValueAbbr ==" ss"){timeMultiplier = 1000; }
// all abbreviations are in MSSQL format: see B.O.L. for datepart()
if (timeValueAbbr == "ss") { timeMultiplier = 1000; }



你还没有对timeMultiplier进行decalred,因此它是一个隐含的全局

变量。在这个函数里面的某个地方你需要说


var timeMultiplier;


You haven''t decalred timeMultiplier and so it is an implied global
variable. Somewhere inside this function you need to say

var timeMultiplier;


if(timeValueAbbr ==" mi"){ timeMultiplier = 1000 * 60; }

if(timeValueAbbr ==" hh"){timeMultiplier = 1000 * 60 * 60; }

if(timeValueAbbr ==" dd"){timeMultiplier = 1000 * 60 * 60 * 24; }
if (timeValueAbbr == "mi") { timeMultiplier = 1000 * 60; }
if (timeValueAbbr == "hh") { timeMultiplier = 1000 * 60 * 60; }
if (timeValueAbbr == "dd") { timeMultiplier = 1000 * 60 * 60 * 24; }



如果前一个条件是正确的,则无需检查这些条件

no need to check these conditionals if a previous one was correct


/ /创建并填充所需的变量

var sTargetURL =" Index.php"

var waitTime = interval * timeMultiplier;

var cacheBuster = parseInt (Math.random()* 9999999); //缓存清除强制服务器加载''新''页面
// create and populate needed variables
var sTargetURL = "Index.php"
var waitTime = interval * timeMultiplier;
var cacheBuster = parseInt( Math.random() * 9999999 ); // "cache busting" forces the server to load a ''new'' page



上面不需要parseInt。 (当使用parseInt时总是使用第二个

radix参数。)


这会强制*浏览器*加载新页面

随机数有可能重复,而不是作为

缓存破坏者。使用日期代替


var cacheBuster =(new Date())。getTime();

No need for parseInt above. (When using parseInt always use the second
radix parameter.)

This forces the *browser* to load a new page

There is a chance the random number will repeat and not work as a
"cache buster". Use Date instead

var cacheBuster = (new Date()).getTime();


function doRedirect(){setTimeout(" timedRedirect()",1 * 700); }
function doRedirect() { setTimeout( "timedRedirect()", 1*700 ); }



我认为这是你的非工作代码的问题。


setTimeout可以带一个字符串或函数第一个论点。

功能选择似乎是出于性能原因的首选,但是

并不会对你造成影响。


使用字符串作为setTimeout的参数,在全局窗口对象的范围内计算字符串是否为
。由于您在redirectPage函数中定义了timedRedirect,因此当评估

字符串时,它无法找到timedRedirect函数。


你可能意味着在第二个参数中涉及waitTime

setTimeout


你也从不调用doRedirect函数所以setTimeout永远不会是

被调用,重定向永远不会发生。

I think this is the problem for your non-working code.

setTimeout can take a string or function as the first argument. The
function choice seems to be preferred for performance reasons but that
doesn''t really affect you here.

When you use a string as the argument to setTimeout, the string is
evaluated in the scope of the global window object. Since you have
defined timedRedirect inside your redirectPage function, when the
string is evaluated it cannot find the timedRedirect function.

You probably meant to involve waitTime in the second argument to
setTimeout

also you never call the doRedirect function so the setTimeout is never
called either and the redirect will never happen.


function timedRedirect(){window.location.href = sTargetURL +"?cb =" + cacheBuster; }

}


// - >
function timedRedirect() { window.location.href = sTargetURL + "?cb=" + cacheBuster; }
}

//-->



不需要上面的评论专栏

Don''t need the above comment line


< / script>
</script>



结合所有这些尝试


< script type =" text / javascript">


函数redirectPage(interval,timeValueAbbr){

//检查空值'并设置默认值

interval = interval || 1;


var timeMultiplier;

//所有缩写均采用MSSQL格式:参见B.O.L. for

datepart()

if(timeValueAbbr ==" mi"){

timeMultiplier = 1000 * 60;

}否则if(timeValueAbbr ==" hh"){

timeMultiplier = 1000 * 60 * 60;

}否则if(timeValueAbbr ==" dd" ;){

timeMultiplier = 1000 * 60 * 60 * 24;

}否则{

timeMultiplier = 1000;

}


//创建并填充所需的变量

var waitTime = interval * timeMultiplier;

var sTargetURL ="的index.php" +(new Date())。getTime(); //缓存

破坏强制浏览器加载新页面


setTimeout(function(){window.location.href = sTargetURL;},

waitTime) ;

}


< / script>


使用< URL:http可以让您的生活更轻松://getfirebug.com安装。


彼得

To combine all this try

<script type="text/javascript">

function redirectPage(interval, timeValueAbbr) {
// check for null value''s and set defaults
interval = interval || 1;

var timeMultiplier;
// all abbreviations are in MSSQL format: see B.O.L. for
datepart()
if (timeValueAbbr == "mi") {
timeMultiplier = 1000 * 60;
} else if (timeValueAbbr == "hh") {
timeMultiplier = 1000 * 60 * 60;
} else if (timeValueAbbr == "dd") {
timeMultiplier = 1000 * 60 * 60 * 24;
} else {
timeMultiplier = 1000;
}

// create and populate needed variables
var waitTime = interval * timeMultiplier;
var sTargetURL = "Index.php" + (new Date()).getTime(); // "cache
busting" forces the browser to load a ''new'' page

setTimeout(function() {window.location.href = sTargetURL;},
waitTime);
}

</script>

Your life will be easier with <URL: http://getfirebug.cominstalled.

Peter


文章< 11 ****** ****************@i13g2000prf.googlegroups .com> ;,

Peter Michaux< pe ********** @ gmail。编写:
In article <11**********************@i13g2000prf.googlegroups .com>,
Peter Michaux <pe**********@gmail.comwrote:

我正在寻找解释为什么一段代码可以工作的原因和

另一个没有。我必须警告你,这是我写过的第一篇

Javascript,所以如果有更好的方法或更简单的答案,那么所有人都可以意思是告诉我光明!
I''m looking for an explanation of why one piece of code works and
another does not. I have to warn you that this is the first piece of
Javascript I''ve ever written, so if there is a better way or a simpler
answer, by all means show me the light!



这可能会有所帮助...


This might be helpful...



[snip]

[snip]


//检查空值并设置默认值

if(interval == null){interval = 1; }
// check for null value''s and set defaults
if (interval == null) { interval = 1; }



如果(!interval){interval = 1;}


You can write
if (!interval) {interval = 1;}

$ b $你可以写

b
另一方面,OP写的更清楚。

On the other hand what the OP wrote is much clearer.


甚至


interval = interval || 1;
or even

interval = interval || 1;



我不确定这是否正确。如果间隔是10,那该怎么办?

I''m not sure if this is even correct. What about if interval was 10?


这篇关于请解释一下我做错了什么......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆