简体   繁体   English

JavaScript拆分方法未按预期工作

[英]Javascript Split method not working as expected

I have a method in javascript that use the split method to store the time in an array and then convert the time to seconds. 我在javascript中有一个方法,该方法使用split方法将时间存储在数组中,然后将时间转换为秒。 But when I debug, the array always has first 2 elements and ignore the last one. 但是,当我调试时,数组始终具有前2个元素,而忽略了后一个元素。 Not sure why? 不知道为什么吗?

GetSeconds : function (time) {                    
    var timesecs = 0;
    var min = 1;                   
    var timeArray = time.split(ctx.options.separator); //this always contain 2 elements

    while (timeArray.length > 0) {
        timesecs += min * parseInt(timeArray.pop());
        min *= 60;
    }
    return timesecs;                    
}

ctx.options.separator is a variable that stores my delimiter. ctx.options.separator是存储我的定界符的变量。 I was trying with ":" and time passed was "00:00:00". 我正在尝试使用“:”,而经过的时间是“ 00:00:00”。 This method is called from another method which increments the second. 从另一个递增第二个方法的方法中调用此方法。

I tried it in IE, Chrome and Firebug. 我在IE,Chrome和Firebug中尝试过。 This behaves differently when I debug through Visual Studio (as this code is in my .net app) 当我通过Visual Studio调试时,此行为会有所不同(因为此代码在我的.net应用程序中)

I tried a fiddle and everything works fine there. 我尝试了一个小提琴 ,那里一切正常。 Are you sure, that ctx.options.separator works as expected? 您确定ctx.options.separator可以正常工作吗?

The problem may be a browser issue if ctx.options.separator is being generated properly. 如果正确生成了ctx.options.separator,则问题可能是浏览器问题。 Which browser are you using? 您正在使用哪个浏览器?

Use this cross browser method that will make everything work as expected no matter which browser you use. 使用此跨浏览器方法,无论使用哪种浏览器,都将使所有功能正常运行。 http://blog.stevenlevithan.com/archives/cross-browser-split http://blog.stevenlevithan.com/archives/cross-browser-split

It always has worked for me. 它一直为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM